public class Cluster extends Object implements AutoCloseable
This class has three main roles.
K2hdkcLibrary
C API and Cmd
classes.
An Usage Example:
Suppose you want to add a key of "keystring" with a value of "valuestring" to a k2hdkc cluster. You could write this as:
package com.example;
import ax.antpick.k2hdkc.*;
import com.sun.jna.*;
import com.sun.jna.ptr.*;
import java.io.IOException;
import java.util.*;
import java.util.stream.*;
public class App {
public static void main(String[] args) {
try (Cluster cluster = Cluster.of("cluster/slave.yaml")) {
cluster.set("keystring", "valstring");
System.out.println(cluster.get("keystring"));
} catch (IOException ex) {
System.out.println(ex.getMessage());
assert (false);
}
}
}
Before running the code above, You should run three processes.
The following commands in this repository will run all processes you need in localhost.
$ cd cluster
$ sh start_server.sh
Modifier and Type | Class and Description |
---|---|
static class |
Cluster.NativeLogLevel
Defines levels used to dump a k2hdkc data
|
static class |
Cluster.NativeStackLogLevel
Defines the level which native library puts logs.
|
Modifier and Type | Field and Description |
---|---|
static boolean |
DEFAULT_CLEANUP
Default clean up
|
static String |
DEFAULT_CUK
The cuk(Cloud Unique Key) a chmpx slave process's cuk
|
static Cluster.NativeLogLevel |
DEFAULT_NATIVE_LOG_LEVEL
The default C library log level
|
static Path |
DEFAULT_NATIVE_LOG_PATH
The default log level
|
static Cluster.NativeStackLogLevel |
DEFAULT_NATIVE_STACK_LOG_LEVEL
The default stack log level
|
static short |
DEFAULT_PORT
The port a chmpx slave process listens to control the
process
|
static boolean |
DEFAULT_REJOIN
true if reconnecting with a chmpx slave process
after unexpected closing a connection. |
static boolean |
DEFAULT_RETRY_REJOIN_FOREVER
Default retry rejoin
|
Modifier and Type | Method and Description |
---|---|
boolean |
clearSubkeys(String key)
Clear subkeys of a key.
|
void |
close()
Closes(Disposes) native libraries called by JNA.
|
<T> Optional<T> |
get(String key)
Retrieves the value of a key.
|
String |
getCuk()
Returns a chmpx slave cuk(Cloud Unique Key) string
|
String |
getPath()
Returns a chmpx cluster configuration file path string
|
short |
getPort()
Returns a chmpx slave control port
|
List<String> |
getSubkeys(String key)
Retrieves the subkeys of a key.
|
void |
initNativeLog()
Initializes log files on each native stack.
|
boolean |
isCleanup()
Returns a flag to clear configuration backup
|
boolean |
isRejoin()
Returns a flag to rejoin cluster automatically
|
boolean |
isRetryRejoinForever()
Returns a flag to do retry to rejoin cluster automatically
|
static Cluster |
of(String path)
Creates a
Cluster instance. |
static Cluster |
of(String path,
short port)
Creates a
Cluster instance. |
static Cluster |
of(String path,
short port,
String cuk)
Creates a
Cluster instance. |
static Cluster |
of(String pathname,
short port,
String cuk,
boolean isRejoin,
boolean isRetryRejoinForever,
boolean isCleanup)
Creates a
Cluster instance. |
boolean |
remove(String key)
Removes the value of a key.
|
boolean |
set(String key,
String val)
Sets the value of a key.
|
boolean |
setNativeLogLevel(String pathname,
Cluster.NativeStackLogLevel stackLogLevel,
Cluster.NativeLogLevel logLevel)
Enables logging on each native stack.
|
boolean |
setSubkeys(String key,
String[] subkeys)
Sets a subkey of a key.
|
String |
toString()
Returns full of members as a string.
|
public static final Path DEFAULT_NATIVE_LOG_PATH
public static final Cluster.NativeLogLevel DEFAULT_NATIVE_LOG_LEVEL
public static final Cluster.NativeStackLogLevel DEFAULT_NATIVE_STACK_LOG_LEVEL
public static final short DEFAULT_PORT
public static final String DEFAULT_CUK
public static final boolean DEFAULT_REJOIN
true
if reconnecting with a chmpx slave process
after unexpected closing a connection.public static final boolean DEFAULT_RETRY_REJOIN_FOREVER
public static final boolean DEFAULT_CLEANUP
public String toString()
public static Cluster of(String path) throws IOException
Cluster
instance.path
- a chmpx slave server configuration file
path stringIllegalArgumentException
- if pathname should not be nullIOException
- if the chmpx slave configuration
file open failedpublic static Cluster of(String path, short port) throws IOException
Cluster
instance.path
- a chmpx slave server configuration file
path stringport
- a chmpx control portIllegalArgumentException
- if pathname should not be nullIOException
- if the chmpx slave configuration
file open failedpublic static Cluster of(String path, short port, String cuk) throws IOException
Cluster
instance.path
- a chmpx slave server configuration file
path stringport
- a chmpx control portcuk
- a chmpx slave cuk(Cloud Unique Key) stringIllegalArgumentException
- if pathname should not be nullIOException
- if the chmpx slave configuration
file open failedpublic static Cluster of(String pathname, short port, String cuk, boolean isRejoin, boolean isRetryRejoinForever, boolean isCleanup) throws IOException
Cluster
instance.pathname
- a chmpx slave server configuration file
path stringport
- a chmpx control portcuk
- a chmpx slave cuk(Cloud Unique Key) stringisRejoin
- true
if try connecting once after failing to connect with a
clusterisRetryRejoinForever
- true
if try connecting with a chmpx slave server forever after failure of connection
with a chmpx slave serverisCleanup
- true
if backup configuration files remove when leaveing from a
clusterIllegalArgumentException
- if pathname should not be nullIOException
- if the chmpx slave configuration
file open failedpublic void close() throws IOException
close
in interface AutoCloseable
IOException
- if underlying library errors occur.public String getPath()
public short getPort()
public String getCuk()
public boolean isRejoin()
true
if try connecting once after failing to connect with a clusterpublic boolean isRetryRejoinForever()
public boolean isCleanup()
true
if backup configuration files remove when leaveing from a clusterpublic <T> Optional<T> get(String key) throws IOException
GetCmd
class. Please note this method returns empty object if cluster
returns empty result.key
- a key stringIllegalArgumentException
- if an illegal augment existsIOException
- if underlying library errors occur.public boolean set(String key, String val) throws IOException
SetCmd
class.key
- a key stringval
- a value stringtrue
if success. false
otherwiseIllegalArgumentException
- if an illegal augment existsIOException
- if underlying library errors occur.public boolean remove(String key) throws IOException
key
- a key stringtrue
if success. false
otherwiseIllegalArgumentException
- if an illegal augment existsIOException
- if underlying library errors occur.public boolean setSubkeys(String key, String[] subkeys) throws IOException
key
- a key stringsubkeys
- an array of subkey stringstrue
if success. false
otherwiseIllegalArgumentException
- if an illegal augment existsIOException
- if underlying library errors occur.public List<String> getSubkeys(String key) throws IOException
key
- a key stringIllegalArgumentException
- if an illegal augment existsIOException
- if underlying library errors occur.public boolean clearSubkeys(String key) throws IOException
key
- a key stringtrue
if success. false
otherwise.IllegalArgumentException
- if an illegal augment existsIOException
- if underlying library errors occur.public void initNativeLog()
public boolean setNativeLogLevel(String pathname, Cluster.NativeStackLogLevel stackLogLevel, Cluster.NativeLogLevel logLevel)
pathname
- a log file string to put logs.stackLogLevel
- a log level of a native libary stacklogLevel
- a log level of the native libarytrue
if success. false
otherwise.IllegalArgumentException
- if an illegal augment existsCopyright © 2020. All rights reserved.