com.despegar.jdbc.galera.GaleraStatus Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of galera-java-client Show documentation
Show all versions of galera-java-client Show documentation
A simple Java client for MariaDB Galera Cluster and Percona XtraDB Cluster. It is designed to be an
alternative to connect JVM app to
MariaDB/Percona galera nodes without HAProxy
package com.despegar.jdbc.galera;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
public class GaleraStatus {
private static final String INCOMING_ADDRESSES = "wsrep_incoming_addresses";
private static final String PRIMARY = "Primary";
private static final String SYNC_WAIT_VARIABLE = "wsrep_sync_wait";
private static final String CAUSAL_READS_VARIABLE = "wsrep_causal_reads";
private static final String CLUSTER_STATUS = "wsrep_cluster_status";
private static final String STATUS_DONOR = "Donor/Desynced";
private static final String STATUS_SYNCED = "Synced";
private static final String STATE_VARIABLE = "wsrep_local_state_comment";
private static final String THREADS_CONNECTED = "Threads_connected";
private final Map statusMap;
public GaleraStatus(Map statusMap) {
this.statusMap = statusMap;
}
public Collection getClusterNodes() {
return Arrays.asList(statusMap.get(INCOMING_ADDRESSES).split(","));
}
public boolean isPrimary() {
return statusMap.get(CLUSTER_STATUS).equals(PRIMARY);
}
public boolean isSynced() {
return state().equals(STATUS_SYNCED);
}
public String state() {
return statusMap.get(STATE_VARIABLE);
}
public boolean isDonor() {
return state().equals(STATUS_DONOR);
}
public boolean supportsSyncWait() {
return statusMap.keySet().contains(SYNC_WAIT_VARIABLE);
}
public Integer threadsConnectedCount() {
return Integer.valueOf(statusMap.get(THREADS_CONNECTED));
}
public String getGlobalConsistencyLevel() {
if (supportsSyncWait()) {
return statusMap.get(SYNC_WAIT_VARIABLE);
}
// Earlier mariadb versions
return statusMap.get(CAUSAL_READS_VARIABLE);
}
public static GaleraStatus buildTestStatusOk(String node) {
Map statusMap = new HashMap();
statusMap.put(CLUSTER_STATUS, PRIMARY);
statusMap.put(STATE_VARIABLE, STATUS_SYNCED);
statusMap.put(INCOMING_ADDRESSES, node);
return new GaleraStatus(statusMap);
}
}