6 package com.vesoft.nebula.client.meta;
8 import com.facebook.thrift.TException;
9 import com.google.common.collect.Maps;
10 import com.vesoft.nebula.HostAddr;
11 import com.vesoft.nebula.client.graph.data.HostAddress;
12 import com.vesoft.nebula.client.graph.data.SSLParam;
13 import com.vesoft.nebula.client.graph.exception.ClientServerIncompatibleException;
14 import com.vesoft.nebula.client.meta.exception.ExecuteFailedException;
15 import com.vesoft.nebula.meta.EdgeItem;
16 import com.vesoft.nebula.meta.IdName;
17 import com.vesoft.nebula.meta.SpaceItem;
18 import com.vesoft.nebula.meta.TagItem;
19 import java.io.Serializable;
20 import java.net.UnknownHostException;
21 import java.util.ArrayList;
22 import java.util.HashMap;
23 import java.util.HashSet;
24 import java.util.List;
27 import java.util.concurrent.locks.ReentrantReadWriteLock;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
35 private class SpaceInfo {
36 private SpaceItem spaceItem =
null;
37 private Map<String, TagItem> tagItems =
new HashMap<>();
38 private Map<Integer, String> tagIdNames =
new HashMap<>();
39 private Map<String, EdgeItem> edgeItems =
new HashMap<>();
40 private Map<Integer, String> edgeTypeNames =
new HashMap<>();
41 private Map<Integer, List<HostAddr>> partsAlloc =
new HashMap<>();
45 private Map<String, Map<Integer, HostAddr>> partLeaders =
null;
47 private static final Logger LOGGER = LoggerFactory.getLogger(
MetaManager.class);
50 private final ReentrantReadWriteLock lock =
new ReentrantReadWriteLock();
52 private static final int DEFAULT_TIMEOUT_MS = 1000;
53 private static final int DEFAULT_CONNECTION_RETRY_SIZE = 3;
54 private static final int DEFAULT_EXECUTION_RETRY_SIZE = 3;
69 public MetaManager(List<HostAddress> address,
int timeout,
int connectionRetry,
70 int executionRetry,
boolean enableSSL,
SSLParam sslParam)
72 metaClient =
new MetaClient(address, timeout, connectionRetry, executionRetry, enableSSL,
90 private void fillMetaInfo() {
93 List<IdName> spaces = metaClient.
getSpaces();
94 for (IdName space : spaces) {
95 SpaceInfo spaceInfo =
new SpaceInfo();
96 String spaceName =
new String(space.name);
97 SpaceItem spaceItem = metaClient.
getSpace(spaceName);
98 spaceInfo.spaceItem = spaceItem;
99 List<TagItem> tags = metaClient.
getTags(spaceName);
100 for (TagItem tag : tags) {
101 String tagName =
new String(tag.tag_name);
102 if (!spaceInfo.tagItems.containsKey(tagName)
103 || spaceInfo.tagItems.get(tagName).getVersion() < tag.getVersion()) {
104 spaceInfo.tagItems.put(tagName, tag);
105 spaceInfo.tagIdNames.put(tag.tag_id, tagName);
108 List<EdgeItem> edges = metaClient.
getEdges(spaceName);
109 for (EdgeItem edge : edges) {
110 String edgeName =
new String(edge.edge_name);
111 if (!spaceInfo.edgeItems.containsKey(edgeName)
112 || spaceInfo.edgeItems.get(edgeName).getVersion() < edge.getVersion()) {
113 spaceInfo.edgeItems.put(edgeName, edge);
114 spaceInfo.edgeTypeNames.put(edge.edge_type, edgeName);
118 tempSpacesInfo.put(spaceName, spaceInfo);
121 lock.writeLock().lock();
122 spacesInfo = tempSpacesInfo;
123 if (partLeaders ==
null) {
124 partLeaders =
new HashMap<>();
126 for (String spaceName : spacesInfo.keySet()) {
127 if (!partLeaders.containsKey(spaceName)) {
128 partLeaders.put(spaceName, Maps.newConcurrentMap());
129 for (
int partId : spacesInfo.get(spaceName).partsAlloc.keySet()) {
130 if (spacesInfo.get(spaceName).partsAlloc.get(partId).size() < 1) {
131 LOGGER.error(
"space {} part {} has not allocation host.",
134 partLeaders.get(spaceName).put(partId,
135 spacesInfo.get(spaceName).partsAlloc.get(partId).get(0));
142 lock.writeLock().unlock();
144 }
catch (TException | ExecuteFailedException e) {
145 LOGGER.error(e.getMessage());
156 public int getSpaceId(String spaceName)
throws IllegalArgumentException {
157 return getSpace(spaceName).space_id;
167 public SpaceItem
getSpace(String spaceName)
throws IllegalArgumentException {
168 if (!spacesInfo.containsKey(spaceName)) {
172 lock.readLock().lock();
173 if (!spacesInfo.containsKey(spaceName)) {
174 throw new IllegalArgumentException(
"space:" + spaceName +
" does not exist.");
176 return spacesInfo.get(spaceName).spaceItem;
178 lock.readLock().unlock();
189 public int getTagId(String spaceName, String tagName)
throws IllegalArgumentException {
190 return getTag(spaceName, tagName).tag_id;
201 public TagItem
getTag(String spaceName, String tagName)
throws IllegalArgumentException {
202 if (!spacesInfo.containsKey(spaceName)
203 || !spacesInfo.get(spaceName).tagItems.containsKey(tagName)) {
207 lock.readLock().lock();
208 if (!spacesInfo.containsKey(spaceName)) {
209 throw new IllegalArgumentException(
"Space:" + spaceName +
" does not exist.");
211 if (!spacesInfo.get(spaceName).tagItems.containsKey(tagName)) {
212 throw new IllegalArgumentException(
"Tag:" + tagName +
" does not exist.");
214 return spacesInfo.get(spaceName).tagItems.get(tagName);
216 lock.readLock().unlock();
228 public int getEdgeType(String spaceName, String edgeName)
throws IllegalArgumentException {
229 return getEdge(spaceName, edgeName).edge_type;
240 public EdgeItem
getEdge(String spaceName, String edgeName)
throws IllegalArgumentException {
241 if (!spacesInfo.containsKey(spaceName)
242 || !spacesInfo.get(spaceName).edgeItems.containsKey(edgeName)) {
246 lock.readLock().lock();
247 if (!spacesInfo.containsKey(spaceName)) {
248 throw new IllegalArgumentException(
"Space:" + spaceName +
" does not exist.");
250 if (!spacesInfo.get(spaceName).edgeItems.containsKey(edgeName)) {
251 throw new IllegalArgumentException(
"Edge:" + edgeName +
" does not exist.");
253 return spacesInfo.get(spaceName).edgeItems.get(edgeName);
255 lock.readLock().unlock();
266 public HostAddr
getLeader(String spaceName,
int part)
throws IllegalArgumentException {
267 if (!spacesInfo.containsKey(spaceName)) {
271 lock.readLock().lock();
272 if (partLeaders ==
null) {
273 throw new IllegalArgumentException(
"Space:" + spaceName +
" does not exist.");
276 if (!partLeaders.containsKey(spaceName)) {
277 throw new IllegalArgumentException(
"Space:" + spaceName +
" does not exist.");
280 if (!partLeaders.get(spaceName).containsKey(part)) {
281 throw new IllegalArgumentException(
"PartId:" + part +
" does not exist.");
283 return partLeaders.get(spaceName).get(part);
285 lock.readLock().unlock();
295 public List<Integer>
getSpaceParts(String spaceName)
throws IllegalArgumentException {
307 throws IllegalArgumentException {
308 if (!spacesInfo.containsKey(spaceName)) {
312 lock.readLock().lock();
313 if (!spacesInfo.containsKey(spaceName)) {
314 throw new IllegalArgumentException(
"Space:" + spaceName +
" does not exist.");
316 return spacesInfo.get(spaceName).partsAlloc;
318 lock.readLock().unlock();
329 public void updateLeader(String spaceName,
int part, HostAddr newLeader)
330 throws IllegalArgumentException {
332 lock.writeLock().lock();
333 if (partLeaders ==
null) {
334 throw new IllegalArgumentException(
"Space:" + spaceName +
" does not exist.");
337 if (!partLeaders.containsKey(spaceName)) {
338 throw new IllegalArgumentException(
"Space:" + spaceName +
" does not exist.");
341 if (!partLeaders.get(spaceName).containsKey(part)) {
342 throw new IllegalArgumentException(
"PartId:" + part +
" does not exist.");
344 partLeaders.get(spaceName).put(part, newLeader);
346 lock.writeLock().unlock();
354 Set<HostAddr> hosts = metaClient.
listHosts();
356 return new HashSet<>();
361 public int getConnectionRetry() {
362 return metaClient.getConnectionRetry();
365 public int getTimeout() {
366 return metaClient.getTimeout();
369 public int getExecutionRetry() {
370 return metaClient.getExecutionRetry();