Skip to content
This repository has been archived by the owner on May 10, 2022. It is now read-only.

add createClient API with clientOptions #49

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/main/java/com/xiaomi/infra/pegasus/client/ClientOptions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) 2019, Xiaomi, Inc. All rights reserved.
// This source code is licensed under the Apache License Version 2.0, which
// can be found in the LICENSE file in the root directory of this source tree.
package com.xiaomi.infra.pegasus.client;

public class ClientOptions {
neverchanje marked this conversation as resolved.
Show resolved Hide resolved
String metaServers = "127.0.0.1:34601,127.0.0.1:34602,127.0.0.1:34603";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

还有几个options都加上吧:

  • async_workers
  • enable_perf_counter
  • perf_counter_tags
  • push_counter_interval_secs

int timeout = 1000;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为了和operation_timeout 保持一致,用operationTimeout更好


public ClientOptions() {}

public ClientOptions(String metaServers, int timeout) {
this.metaServers = metaServers;
this.timeout = timeout;
}

@Override
public boolean equals(Object options) {
if (this == options) {
return true;
}
if (options instanceof ClientOptions) {
ClientOptions clientOptions = (ClientOptions) options;
if (this.metaServers.equals(clientOptions.metaServers)
&& this.timeout == clientOptions.timeout) {
return true;
}
}
return false;
}

@Override
public String toString() {
return "ClientOptions{" + "metaServers='" + metaServers + '\'' + ", timeout=" + timeout + '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// can be found in the LICENSE file in the root directory of this source tree.
package com.xiaomi.infra.pegasus.client;

import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -17,12 +18,21 @@ public class PegasusClientFactory {
private static String singletonClientConfigPath = null;
private static Object singletonClientLock = new Object();

private static ClientOptions singletonClientOptions = null;

// Create a client instance.
// After used, should call client.close() to release resource.
public static PegasusClientInterface createClient(String configPath) throws PException {
return new PegasusClient(configPath);
}

public static PegasusClientInterface createClient(ClientOptions options) throws PException {
Properties pegasusConfig = new Properties();
pegasusConfig.setProperty("meta_servers", options.metaServers);
pegasusConfig.setProperty("operation_timeout", String.valueOf(options.timeout));
return new PegasusClient(pegasusConfig);
}

// Get the singleton client instance with default config path of "resource:///pegasus.properties".
public static PegasusClientInterface getSingletonClient() throws PException {
return getSingletonClient("resource:///pegasus.properties");
Expand Down Expand Up @@ -58,6 +68,29 @@ public static PegasusClientInterface getSingletonClient(String configPath) throw
}
}

public static PegasusClientInterface getSingletonClient(ClientOptions options) throws PException {
synchronized (singletonClientLock) {
if (singletonClient == null) {
try {
singletonClient = (PegasusClient) createClient(options);
singletonClientOptions = options;
LOGGER.info("Create Singleton PegasusClient with options \"" + options.toString() + "\"");
} catch (Throwable e) {
throw new PException("Create Singleton PegasusClient Failed", e);
}
} else if (!singletonClientOptions.equals(options)) {
LOGGER.error(
"Singleton PegasusClient options Conflict: \""
+ options.toString()
+ "\" VS \""
+ singletonClientOptions.toString()
+ "\"");
throw new PException("Singleton PegasusClient options Conflict");
}
return singletonClient;
}
}

// Close the singleton client instance.
public static void closeSingletonClient() throws PException {
synchronized (singletonClientLock) {
Expand Down
76 changes: 76 additions & 0 deletions src/test/java/com/xiaomi/infra/pegasus/client/TestBasic.java
Original file line number Diff line number Diff line change
Expand Up @@ -2250,4 +2250,80 @@ public void fullScanWithFilter() throws PException {

PegasusClientFactory.closeSingletonClient();
}

@Test
public void createClient() throws PException {
System.out.println("test createClient with clientOptions");
ClientOptions clientOptions = new ClientOptions();
byte[] value = null;
// test createClient(clientOptions)
PegasusClientInterface client = null;
try {
client = PegasusClientFactory.createClient(clientOptions);
client.set("temp", "createClient".getBytes(), "0".getBytes(), "0".getBytes());
value = client.get("temp", "createClient".getBytes(), "0".getBytes());
} catch (Exception e) {
e.printStackTrace();
Assert.assertTrue(false);
}

Assert.assertTrue(new String(value).equals("0"));

// test getSingletonClient(ClientOptions options)
PegasusClientInterface singletonClient = null;
try {
singletonClient = PegasusClientFactory.getSingletonClient(clientOptions);
singletonClient.set("temp", "getSingletonClient".getBytes(), "0".getBytes(), "0".getBytes());
value = singletonClient.get("temp", "getSingletonClient".getBytes(), "0".getBytes());
} catch (Exception e) {
e.printStackTrace();
Assert.assertTrue(false);
}

Assert.assertTrue(new String(value).equals("0"));

// test getSingletonClient(ClientOptions options) --> same clientOptions
PegasusClientInterface singletonClient1 = null;
try {
singletonClient1 = PegasusClientFactory.getSingletonClient(clientOptions);
singletonClient1.set("temp", "getSingletonClient".getBytes(), "1".getBytes(), "1".getBytes());
value = singletonClient1.get("temp", "getSingletonClient".getBytes(), "1".getBytes());
} catch (Exception e) {
e.printStackTrace();
Assert.assertTrue(false);
}

Assert.assertTrue(new String(value).equals("1"));
Assert.assertTrue(singletonClient1 == singletonClient);

// test getSingletonClient(ClientOptions options) --> different clientOptions,but values of
// clientOptions is same
ClientOptions clientOptions1 = new ClientOptions();
try {
singletonClient1 = PegasusClientFactory.getSingletonClient(clientOptions1);
singletonClient1.set("temp", "getSingletonClient".getBytes(), "2".getBytes(), "2".getBytes());
value = singletonClient1.get("temp", "getSingletonClient".getBytes(), "2".getBytes());
} catch (Exception e) {
e.printStackTrace();
Assert.assertTrue(false);
}

Assert.assertTrue(new String(value).equals("2"));
Assert.assertTrue(singletonClient1 == singletonClient);

// test getSingletonClient(ClientOptions options) --> different clientOptions,and values of
// clientOptions is different
ClientOptions clientOptions2 = new ClientOptions();
clientOptions2.timeout = 10000;
try {
singletonClient1 = PegasusClientFactory.getSingletonClient(clientOptions2);
singletonClient1.set("temp", "getSingletonClient".getBytes(), "2".getBytes(), "2".getBytes());
value = singletonClient1.get("temp", "getSingletonClient".getBytes(), "2".getBytes());
} catch (Exception e) {
// if values of clientOptions is different,the code's right logic is "throw exception"
Assert.assertTrue(true);
}

PegasusClientFactory.closeSingletonClient();
}
}