NebulaGraph Java Client  release-3.6
All Classes Functions Variables
ConnObjectPool.java
1 package com.vesoft.nebula.client.graph.net;
2 
3 import com.vesoft.nebula.client.graph.NebulaPoolConfig;
4 import com.vesoft.nebula.client.graph.data.HostAddress;
5 import com.vesoft.nebula.client.graph.exception.ClientServerIncompatibleException;
6 import com.vesoft.nebula.client.graph.exception.IOErrorException;
7 import java.io.Serializable;
8 import org.apache.commons.pool2.BasePooledObjectFactory;
9 import org.apache.commons.pool2.PooledObject;
10 import org.apache.commons.pool2.impl.DefaultPooledObject;
11 
12 public class ConnObjectPool extends BasePooledObjectFactory<SyncConnection>
13  implements Serializable {
14 
15  private static final long serialVersionUID = 6101157301791971560L;
16 
17  private final NebulaPoolConfig config;
18  private final LoadBalancer loadBalancer;
19  private static final int retryTime = 3;
20 
21  public ConnObjectPool(LoadBalancer loadBalancer, NebulaPoolConfig config) {
22  this.loadBalancer = loadBalancer;
23  this.config = config;
24  }
25 
26  @Override
28  HostAddress address = loadBalancer.getAddress();
29  if (address == null) {
30  throw new IOErrorException(IOErrorException.E_ALL_BROKEN,
31  "All servers are broken.");
32  }
33  int retry = retryTime;
34  SyncConnection conn = new SyncConnection();
35  while (retry-- > 0) {
36  try {
37  if (config.isEnableSsl()) {
38  if (config.getSslParam() == null) {
39  throw new IllegalArgumentException("SSL Param is required when enableSsl "
40  + "is set to true");
41  }
42  conn.open(address, config.getTimeout(),
43  config.getSslParam(), config.isUseHttp2(), config.getCustomHeaders());
44  } else {
45  conn.open(address, config.getTimeout(),
46  config.isUseHttp2(), config.getCustomHeaders());
47  }
48  return conn;
49  } catch (IOErrorException e) {
50  if (retry == 0) {
51  throw e;
52  }
53  this.loadBalancer.updateServersStatus();
54  }
55  }
56  return null;
57  }
58 
59  @Override
60  public PooledObject<SyncConnection> wrap(SyncConnection connection) {
61  return new DefaultPooledObject<>(connection);
62  }
63 
64  @Override
65  public void destroyObject(PooledObject<SyncConnection> p) throws Exception {
66  p.getObject().close();
67  // TODO: update the server connection num into load balancer
68  super.destroyObject(p);
69  }
70 
71  @Override
72  public boolean validateObject(PooledObject<SyncConnection> p) {
73  if (p.getObject() == null) {
74  return false;
75  }
76  if (!p.getObject().ping()) {
77  p.getObject().close();
78  return false;
79  }
80  return true;
81  }
82 
83  @Override
84  public void activateObject(PooledObject<SyncConnection> p) throws Exception {
85  if (p.getObject() == null) {
86  throw new RuntimeException("The connection is null.");
87  }
88  if (!p.getObject().ping()) {
89  throw new RuntimeException("The connection is broken.");
90  }
91  super.activateObject(p);
92  }
93 
94  public boolean init() {
95  return loadBalancer.isServersOK();
96  }
97 
98  public void updateServerStatus() {
99  loadBalancer.updateServersStatus();
100  }
101 }