NebulaGraph Java Client  release-3.8
All Classes Functions Variables
StorageConnPoolFactory.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.storage;
7 
8 import com.vesoft.nebula.client.graph.data.HostAddress;
9 import java.io.Serializable;
10 import org.apache.commons.pool2.KeyedPooledObjectFactory;
11 import org.apache.commons.pool2.PooledObject;
12 import org.apache.commons.pool2.impl.DefaultPooledObject;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
15 
16 @SuppressWarnings("checkstyle:Indentation")
18  implements KeyedPooledObjectFactory<HostAddress, GraphStorageConnection>, Serializable {
19  private static final Logger LOGGER = LoggerFactory.getLogger(StorageConnPoolFactory.class);
20 
21  private final StoragePoolConfig config;
22 
24  this.config = config;
25  }
26 
27  @Override
28  public PooledObject<GraphStorageConnection> makeObject(HostAddress address) throws Exception {
30  connection.open(
31  address,
32  config.getTimeout(),
33  config.isEnableSSL(),
34  config.getSslParam());
35  return new DefaultPooledObject<>(connection);
36  }
37 
38  @Override
39  public void destroyObject(HostAddress hostAndPort,
40  PooledObject<GraphStorageConnection> pooledObject) {
41  pooledObject.getObject().close();
42  }
43 
44  @Override
45  public boolean validateObject(HostAddress hostAndPort,
46  PooledObject<GraphStorageConnection> pooledObject) {
47  GraphStorageConnection connection = pooledObject.getObject();
48  if (connection == null) {
49  return false;
50  }
51  try {
52  return connection.transport.isOpen();
53  } catch (Exception e) {
54  LOGGER.warn(String.format("storage connection with %s:%d is not open",
55  hostAndPort.getHost(), hostAndPort.getPort()), e);
56  return false;
57  }
58  }
59 
60  @Override
61  public void activateObject(HostAddress address,
62  PooledObject<GraphStorageConnection> pooledObject)
63  throws Exception {
64  if (pooledObject.getObject() == null) {
65  throw new RuntimeException("The connection is null.");
66  }
67  if (!pooledObject.getObject().transport.isOpen()) {
68  throw new RuntimeException("The connection is broken.");
69  }
70  }
71 
72  @Override
73  public void passivateObject(HostAddress hostAndPort,
74  PooledObject<GraphStorageConnection> pooledObject) {
75  pooledObject.markReturning();
76  }
77 }