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,
89 private void fillMetaInfo() {
92 List<IdName> spaces = metaClient.
getSpaces();
93 for (IdName space : spaces) {
94 SpaceInfo spaceInfo =
new SpaceInfo();
95 String spaceName =
new String(space.name);
96 SpaceItem spaceItem = metaClient.
getSpace(spaceName);
97 spaceInfo.spaceItem = spaceItem;
98 List<TagItem> tags = metaClient.
getTags(spaceName);
99 for (TagItem tag : tags) {
100 String tagName =
new String(tag.tag_name);
101 if (!spaceInfo.tagItems.containsKey(tagName)
102 || spaceInfo.tagItems.get(tagName).getVersion() < tag.getVersion()) {
103 spaceInfo.tagItems.put(tagName, tag);
104 spaceInfo.tagIdNames.put(tag.tag_id, tagName);
107 List<EdgeItem> edges = metaClient.
getEdges(spaceName);
108 for (EdgeItem edge : edges) {
109 String edgeName =
new String(edge.edge_name);
110 if (!spaceInfo.edgeItems.containsKey(edgeName)
111 || spaceInfo.edgeItems.get(edgeName).getVersion() < edge.getVersion()) {
112 spaceInfo.edgeItems.put(edgeName, edge);
113 spaceInfo.edgeTypeNames.put(edge.edge_type, edgeName);
117 tempSpacesInfo.put(spaceName, spaceInfo);
120 lock.writeLock().lock();
121 spacesInfo = tempSpacesInfo;
122 if (partLeaders ==
null) {
123 partLeaders =
new HashMap<>();
125 for (String spaceName : spacesInfo.keySet()) {
126 if (!partLeaders.containsKey(spaceName)) {
127 partLeaders.put(spaceName, Maps.newConcurrentMap());
128 for (
int partId : spacesInfo.get(spaceName).partsAlloc.keySet()) {
129 if (spacesInfo.get(spaceName).partsAlloc.get(partId).size() < 1) {
130 LOGGER.error(
"space {} part {} has not allocation host.",
133 partLeaders.get(spaceName).put(partId,
134 spacesInfo.get(spaceName).partsAlloc.get(partId).get(0));
141 lock.writeLock().unlock();
143 }
catch (TException | ExecuteFailedException e) {
144 LOGGER.error(e.getMessage());
155 public int getSpaceId(String spaceName)
throws IllegalArgumentException {
156 return getSpace(spaceName).space_id;
166 public SpaceItem
getSpace(String spaceName)
throws IllegalArgumentException {
167 if (!spacesInfo.containsKey(spaceName)) {
171 lock.readLock().lock();
172 if (!spacesInfo.containsKey(spaceName)) {
173 throw new IllegalArgumentException(
"space:" + spaceName +
" does not exist.");
175 return spacesInfo.get(spaceName).spaceItem;
177 lock.readLock().unlock();
188 public int getTagId(String spaceName, String tagName)
throws IllegalArgumentException {
189 return getTag(spaceName, tagName).tag_id;
200 public TagItem
getTag(String spaceName, String tagName)
throws IllegalArgumentException {
201 if (!spacesInfo.containsKey(spaceName)
202 || !spacesInfo.get(spaceName).tagItems.containsKey(tagName)) {
206 lock.readLock().lock();
207 if (!spacesInfo.containsKey(spaceName)) {
208 throw new IllegalArgumentException(
"Space:" + spaceName +
" does not exist.");
210 if (!spacesInfo.get(spaceName).tagItems.containsKey(tagName)) {
211 throw new IllegalArgumentException(
"Tag:" + tagName +
" does not exist.");
213 return spacesInfo.get(spaceName).tagItems.get(tagName);
215 lock.readLock().unlock();
227 public int getEdgeType(String spaceName, String edgeName)
throws IllegalArgumentException {
228 return getEdge(spaceName, edgeName).edge_type;
239 public EdgeItem
getEdge(String spaceName, String edgeName)
throws IllegalArgumentException {
240 if (!spacesInfo.containsKey(spaceName)
241 || !spacesInfo.get(spaceName).edgeItems.containsKey(edgeName)) {
245 lock.readLock().lock();
246 if (!spacesInfo.containsKey(spaceName)) {
247 throw new IllegalArgumentException(
"Space:" + spaceName +
" does not exist.");
249 if (!spacesInfo.get(spaceName).edgeItems.containsKey(edgeName)) {
250 throw new IllegalArgumentException(
"Edge:" + edgeName +
" does not exist.");
252 return spacesInfo.get(spaceName).edgeItems.get(edgeName);
254 lock.readLock().unlock();
265 public HostAddr
getLeader(String spaceName,
int part)
throws IllegalArgumentException {
266 if (!spacesInfo.containsKey(spaceName)) {
270 lock.readLock().lock();
271 if (partLeaders ==
null) {
272 throw new IllegalArgumentException(
"Space:" + spaceName +
" does not exist.");
275 if (!partLeaders.containsKey(spaceName)) {
276 throw new IllegalArgumentException(
"Space:" + spaceName +
" does not exist.");
279 if (!partLeaders.get(spaceName).containsKey(part)) {
280 throw new IllegalArgumentException(
"PartId:" + part +
" does not exist.");
282 return partLeaders.get(spaceName).get(part);
284 lock.readLock().unlock();
294 public List<Integer>
getSpaceParts(String spaceName)
throws IllegalArgumentException {
306 throws IllegalArgumentException {
307 if (!spacesInfo.containsKey(spaceName)) {
311 lock.readLock().lock();
312 if (!spacesInfo.containsKey(spaceName)) {
313 throw new IllegalArgumentException(
"Space:" + spaceName +
" does not exist.");
315 return spacesInfo.get(spaceName).partsAlloc;
317 lock.readLock().unlock();
328 public void updateLeader(String spaceName,
int part, HostAddr newLeader)
329 throws IllegalArgumentException {
331 lock.writeLock().lock();
332 if (partLeaders ==
null) {
333 throw new IllegalArgumentException(
"Space:" + spaceName +
" does not exist.");
336 if (!partLeaders.containsKey(spaceName)) {
337 throw new IllegalArgumentException(
"Space:" + spaceName +
" does not exist.");
340 if (!partLeaders.get(spaceName).containsKey(part)) {
341 throw new IllegalArgumentException(
"PartId:" + part +
" does not exist.");
343 partLeaders.get(spaceName).put(part, newLeader);
345 lock.writeLock().unlock();
353 Set<HostAddr> hosts = metaClient.
listHosts();
355 return new HashSet<>();
360 public int getConnectionRetry() {
361 return metaClient.getConnectionRetry();
364 public int getTimeout() {
365 return metaClient.getTimeout();
368 public int getExecutionRetry() {
369 return metaClient.getExecutionRetry();