All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.despegar.jdbc.galera.GaleraStatus Maven / Gradle / Ivy

Go to download

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

There is a newer version: 1.0.20
Show newest version
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 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 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);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy