NebulaGraph Java Client  release-3.8
MetaManager.java
1 /* Copyright (c) 2020 vesoft inc. All rights reserved.
2  *
3  * This source code is licensed under Apache 2.0 License.
4  */
5 
6 package com.vesoft.nebula.client.meta;
7 
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;
25 import java.util.Map;
26 import java.util.Set;
27 import java.util.concurrent.locks.ReentrantReadWriteLock;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 
34 public class MetaManager implements MetaCache, Serializable {
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<>();
42  }
43 
44  private Map<String, MetaManager.SpaceInfo> spacesInfo = new HashMap<>();
45  private Map<String, Map<Integer, HostAddr>> partLeaders = null;
46 
47  private static final Logger LOGGER = LoggerFactory.getLogger(MetaManager.class);
48 
49  private MetaClient metaClient;
50  private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
51 
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;
55 
59  public MetaManager(List<HostAddress> address)
60  throws TException, ClientServerIncompatibleException, UnknownHostException {
61  metaClient = new MetaClient(address);
62  metaClient.connect();
63  fillMetaInfo();
64  }
65 
69  public MetaManager(List<HostAddress> address, int timeout, int connectionRetry,
70  int executionRetry, boolean enableSSL, SSLParam sslParam)
71  throws TException, ClientServerIncompatibleException, UnknownHostException {
72  metaClient = new MetaClient(address, timeout, connectionRetry, executionRetry, enableSSL,
73  sslParam);
74  metaClient.connect();
75  fillMetaInfo();
76  }
77 
78 
82  public void close() {
83  metaClient.close();
84  }
85 
86 
90  private void fillMetaInfo() {
91  try {
92  Map<String, MetaManager.SpaceInfo> tempSpacesInfo = new HashMap<>();
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);
106  }
107  }
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);
115  }
116  }
117  spaceInfo.partsAlloc = metaClient.getPartsAlloc(spaceName);
118  tempSpacesInfo.put(spaceName, spaceInfo);
119  }
120  try {
121  lock.writeLock().lock();
122  spacesInfo = tempSpacesInfo;
123  if (partLeaders == null) {
124  partLeaders = new HashMap<>();
125  }
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.",
132  spaceName, partId);
133  } else {
134  partLeaders.get(spaceName).put(partId,
135  spacesInfo.get(spaceName).partsAlloc.get(partId).get(0));
136  }
137 
138  }
139  }
140  }
141  } finally {
142  lock.writeLock().unlock();
143  }
144  } catch (TException | ExecuteFailedException e) {
145  LOGGER.error(e.getMessage());
146  }
147  }
148 
149 
156  public int getSpaceId(String spaceName) throws IllegalArgumentException {
157  return getSpace(spaceName).space_id;
158  }
159 
166  @Override
167  public SpaceItem getSpace(String spaceName) throws IllegalArgumentException {
168  if (!spacesInfo.containsKey(spaceName)) {
169  fillMetaInfo();
170  }
171  try {
172  lock.readLock().lock();
173  if (!spacesInfo.containsKey(spaceName)) {
174  throw new IllegalArgumentException("space:" + spaceName + " does not exist.");
175  }
176  return spacesInfo.get(spaceName).spaceItem;
177  } finally {
178  lock.readLock().unlock();
179  }
180  }
181 
189  public int getTagId(String spaceName, String tagName) throws IllegalArgumentException {
190  return getTag(spaceName, tagName).tag_id;
191  }
192 
200  @Override
201  public TagItem getTag(String spaceName, String tagName) throws IllegalArgumentException {
202  if (!spacesInfo.containsKey(spaceName)
203  || !spacesInfo.get(spaceName).tagItems.containsKey(tagName)) {
204  fillMetaInfo();
205  }
206  try {
207  lock.readLock().lock();
208  if (!spacesInfo.containsKey(spaceName)) {
209  throw new IllegalArgumentException("Space:" + spaceName + " does not exist.");
210  }
211  if (!spacesInfo.get(spaceName).tagItems.containsKey(tagName)) {
212  throw new IllegalArgumentException("Tag:" + tagName + " does not exist.");
213  }
214  return spacesInfo.get(spaceName).tagItems.get(tagName);
215  } finally {
216  lock.readLock().unlock();
217  }
218  }
219 
220 
228  public int getEdgeType(String spaceName, String edgeName) throws IllegalArgumentException {
229  return getEdge(spaceName, edgeName).edge_type;
230  }
231 
239  @Override
240  public EdgeItem getEdge(String spaceName, String edgeName) throws IllegalArgumentException {
241  if (!spacesInfo.containsKey(spaceName)
242  || !spacesInfo.get(spaceName).edgeItems.containsKey(edgeName)) {
243  fillMetaInfo();
244  }
245  try {
246  lock.readLock().lock();
247  if (!spacesInfo.containsKey(spaceName)) {
248  throw new IllegalArgumentException("Space:" + spaceName + " does not exist.");
249  }
250  if (!spacesInfo.get(spaceName).edgeItems.containsKey(edgeName)) {
251  throw new IllegalArgumentException("Edge:" + edgeName + " does not exist.");
252  }
253  return spacesInfo.get(spaceName).edgeItems.get(edgeName);
254  } finally {
255  lock.readLock().unlock();
256  }
257  }
258 
266  public HostAddr getLeader(String spaceName, int part) throws IllegalArgumentException {
267  if (!spacesInfo.containsKey(spaceName)) {
268  fillMetaInfo();
269  }
270  try {
271  lock.readLock().lock();
272  if (partLeaders == null) {
273  throw new IllegalArgumentException("Space:" + spaceName + " does not exist.");
274  }
275 
276  if (!partLeaders.containsKey(spaceName)) {
277  throw new IllegalArgumentException("Space:" + spaceName + " does not exist.");
278  }
279 
280  if (!partLeaders.get(spaceName).containsKey(part)) {
281  throw new IllegalArgumentException("PartId:" + part + " does not exist.");
282  }
283  return partLeaders.get(spaceName).get(part);
284  } finally {
285  lock.readLock().unlock();
286  }
287  }
288 
295  public List<Integer> getSpaceParts(String spaceName) throws IllegalArgumentException {
296  return new ArrayList<>(getPartsAlloc(spaceName).keySet());
297  }
298 
305  @Override
306  public Map<Integer, List<HostAddr>> getPartsAlloc(String spaceName)
307  throws IllegalArgumentException {
308  if (!spacesInfo.containsKey(spaceName)) {
309  fillMetaInfo();
310  }
311  try {
312  lock.readLock().lock();
313  if (!spacesInfo.containsKey(spaceName)) {
314  throw new IllegalArgumentException("Space:" + spaceName + " does not exist.");
315  }
316  return spacesInfo.get(spaceName).partsAlloc;
317  } finally {
318  lock.readLock().unlock();
319  }
320  }
321 
329  public void updateLeader(String spaceName, int part, HostAddr newLeader)
330  throws IllegalArgumentException {
331  try {
332  lock.writeLock().lock();
333  if (partLeaders == null) {
334  throw new IllegalArgumentException("Space:" + spaceName + " does not exist.");
335  }
336 
337  if (!partLeaders.containsKey(spaceName)) {
338  throw new IllegalArgumentException("Space:" + spaceName + " does not exist.");
339  }
340 
341  if (!partLeaders.get(spaceName).containsKey(part)) {
342  throw new IllegalArgumentException("PartId:" + part + " does not exist.");
343  }
344  partLeaders.get(spaceName).put(part, newLeader);
345  } finally {
346  lock.writeLock().unlock();
347  }
348  }
349 
353  public Set<HostAddr> listHosts() {
354  Set<HostAddr> hosts = metaClient.listHosts();
355  if (hosts == null) {
356  return new HashSet<>();
357  }
358  return hosts;
359  }
360 
361  public int getConnectionRetry() {
362  return metaClient.getConnectionRetry();
363  }
364 
365  public int getTimeout() {
366  return metaClient.getTimeout();
367  }
368 
369  public int getExecutionRetry() {
370  return metaClient.getExecutionRetry();
371  }
372 }
synchronized SpaceItem getSpace(String spaceName)
get one space
synchronized Set< HostAddr > listHosts()
get all Storaged servers
synchronized List< IdName > getSpaces()
get all spaces
synchronized List< TagItem > getTags(String spaceName)
get all tags of spaceName
synchronized Map< Integer, List< HostAddr > > getPartsAlloc(String spaceName)
Get all parts and the address in a space Store in this.parts.
synchronized List< EdgeItem > getEdges(String spaceName)
get all edges of specific space
MetaManager is a manager for meta info, such as spaces,tags and edges.
Set< HostAddr > listHosts()
get all storage addresses
Map< Integer, List< HostAddr > > getPartsAlloc(String spaceName)
get all parts alloc of one space
EdgeItem getEdge(String spaceName, String edgeName)
get Edge
MetaManager(List< HostAddress > address, int timeout, int connectionRetry, int executionRetry, boolean enableSSL, SSLParam sslParam)
init the meta info cache with more config
int getTagId(String spaceName, String tagName)
get tag id
int getSpaceId(String spaceName)
get space id
TagItem getTag(String spaceName, String tagName)
get tag
List< Integer > getSpaceParts(String spaceName)
get all parts of one space
int getEdgeType(String spaceName, String edgeName)
get edge type
SpaceItem getSpace(String spaceName)
get space item
MetaManager(List< HostAddress > address)
init the meta info cache
void updateLeader(String spaceName, int part, HostAddr newLeader)
cache new leader for part
HostAddr getLeader(String spaceName, int part)
get part leader