NebulaGraph Java Client  release-3.6
All Classes Functions Variables
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 
81  public void close() {
82  metaClient.close();
83  }
84 
85 
89  private void fillMetaInfo() {
90  try {
91  Map<String, MetaManager.SpaceInfo> tempSpacesInfo = new HashMap<>();
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);
105  }
106  }
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);
114  }
115  }
116  spaceInfo.partsAlloc = metaClient.getPartsAlloc(spaceName);
117  tempSpacesInfo.put(spaceName, spaceInfo);
118  }
119  try {
120  lock.writeLock().lock();
121  spacesInfo = tempSpacesInfo;
122  if (partLeaders == null) {
123  partLeaders = new HashMap<>();
124  }
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.",
131  spaceName, partId);
132  } else {
133  partLeaders.get(spaceName).put(partId,
134  spacesInfo.get(spaceName).partsAlloc.get(partId).get(0));
135  }
136 
137  }
138  }
139  }
140  } finally {
141  lock.writeLock().unlock();
142  }
143  } catch (TException | ExecuteFailedException e) {
144  LOGGER.error(e.getMessage());
145  }
146  }
147 
148 
155  public int getSpaceId(String spaceName) throws IllegalArgumentException {
156  return getSpace(spaceName).space_id;
157  }
158 
165  @Override
166  public SpaceItem getSpace(String spaceName) throws IllegalArgumentException {
167  if (!spacesInfo.containsKey(spaceName)) {
168  fillMetaInfo();
169  }
170  try {
171  lock.readLock().lock();
172  if (!spacesInfo.containsKey(spaceName)) {
173  throw new IllegalArgumentException("space:" + spaceName + " does not exist.");
174  }
175  return spacesInfo.get(spaceName).spaceItem;
176  } finally {
177  lock.readLock().unlock();
178  }
179  }
180 
188  public int getTagId(String spaceName, String tagName) throws IllegalArgumentException {
189  return getTag(spaceName, tagName).tag_id;
190  }
191 
199  @Override
200  public TagItem getTag(String spaceName, String tagName) throws IllegalArgumentException {
201  if (!spacesInfo.containsKey(spaceName)
202  || !spacesInfo.get(spaceName).tagItems.containsKey(tagName)) {
203  fillMetaInfo();
204  }
205  try {
206  lock.readLock().lock();
207  if (!spacesInfo.containsKey(spaceName)) {
208  throw new IllegalArgumentException("Space:" + spaceName + " does not exist.");
209  }
210  if (!spacesInfo.get(spaceName).tagItems.containsKey(tagName)) {
211  throw new IllegalArgumentException("Tag:" + tagName + " does not exist.");
212  }
213  return spacesInfo.get(spaceName).tagItems.get(tagName);
214  } finally {
215  lock.readLock().unlock();
216  }
217  }
218 
219 
227  public int getEdgeType(String spaceName, String edgeName) throws IllegalArgumentException {
228  return getEdge(spaceName, edgeName).edge_type;
229  }
230 
238  @Override
239  public EdgeItem getEdge(String spaceName, String edgeName) throws IllegalArgumentException {
240  if (!spacesInfo.containsKey(spaceName)
241  || !spacesInfo.get(spaceName).edgeItems.containsKey(edgeName)) {
242  fillMetaInfo();
243  }
244  try {
245  lock.readLock().lock();
246  if (!spacesInfo.containsKey(spaceName)) {
247  throw new IllegalArgumentException("Space:" + spaceName + " does not exist.");
248  }
249  if (!spacesInfo.get(spaceName).edgeItems.containsKey(edgeName)) {
250  throw new IllegalArgumentException("Edge:" + edgeName + " does not exist.");
251  }
252  return spacesInfo.get(spaceName).edgeItems.get(edgeName);
253  } finally {
254  lock.readLock().unlock();
255  }
256  }
257 
265  public HostAddr getLeader(String spaceName, int part) throws IllegalArgumentException {
266  if (!spacesInfo.containsKey(spaceName)) {
267  fillMetaInfo();
268  }
269  try {
270  lock.readLock().lock();
271  if (partLeaders == null) {
272  throw new IllegalArgumentException("Space:" + spaceName + " does not exist.");
273  }
274 
275  if (!partLeaders.containsKey(spaceName)) {
276  throw new IllegalArgumentException("Space:" + spaceName + " does not exist.");
277  }
278 
279  if (!partLeaders.get(spaceName).containsKey(part)) {
280  throw new IllegalArgumentException("PartId:" + part + " does not exist.");
281  }
282  return partLeaders.get(spaceName).get(part);
283  } finally {
284  lock.readLock().unlock();
285  }
286  }
287 
294  public List<Integer> getSpaceParts(String spaceName) throws IllegalArgumentException {
295  return new ArrayList<>(getPartsAlloc(spaceName).keySet());
296  }
297 
304  @Override
305  public Map<Integer, List<HostAddr>> getPartsAlloc(String spaceName)
306  throws IllegalArgumentException {
307  if (!spacesInfo.containsKey(spaceName)) {
308  fillMetaInfo();
309  }
310  try {
311  lock.readLock().lock();
312  if (!spacesInfo.containsKey(spaceName)) {
313  throw new IllegalArgumentException("Space:" + spaceName + " does not exist.");
314  }
315  return spacesInfo.get(spaceName).partsAlloc;
316  } finally {
317  lock.readLock().unlock();
318  }
319  }
320 
328  public void updateLeader(String spaceName, int part, HostAddr newLeader)
329  throws IllegalArgumentException {
330  try {
331  lock.writeLock().lock();
332  if (partLeaders == null) {
333  throw new IllegalArgumentException("Space:" + spaceName + " does not exist.");
334  }
335 
336  if (!partLeaders.containsKey(spaceName)) {
337  throw new IllegalArgumentException("Space:" + spaceName + " does not exist.");
338  }
339 
340  if (!partLeaders.get(spaceName).containsKey(part)) {
341  throw new IllegalArgumentException("PartId:" + part + " does not exist.");
342  }
343  partLeaders.get(spaceName).put(part, newLeader);
344  } finally {
345  lock.writeLock().unlock();
346  }
347  }
348 
352  public Set<HostAddr> listHosts() {
353  Set<HostAddr> hosts = metaClient.listHosts();
354  if (hosts == null) {
355  return new HashSet<>();
356  }
357  return hosts;
358  }
359 
360  public int getConnectionRetry() {
361  return metaClient.getConnectionRetry();
362  }
363 
364  public int getTimeout() {
365  return metaClient.getTimeout();
366  }
367 
368  public int getExecutionRetry() {
369  return metaClient.getExecutionRetry();
370  }
371 }
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