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

com.alachisoft.ncache.client.internal.communication.StartBalancingClients Maven / Gradle / Ivy

There is a newer version: 5.3.0
Show newest version
package com.alachisoft.ncache.client.internal.communication;

import com.alachisoft.ncache.client.ServerInfo;
import com.alachisoft.ncache.client.internal.command.CommandResponse;

import Alachisoft.NCache.Common.Net.Address;

class StartBalancingClients implements Runnable {

    private Broker _parent;
    private CommandResponse response;

    @Override
    public void run() {
        balancingClients();
    }

    private void balancingClients() {

        try {
            Address clusterAddress = null;
            Address serverAddress = null;


            //
            //Copied entire logic from ProcessResponse->Node_Joined_Event to this method because of Azure Common Client.
            //Sir Taimoor's Decision
            //
            ServerInfo newServerJoined = new ServerInfo();

            if (response.getServerPort() > 0) {

                if (_parent._clientConfig.getIPMappingConfigured()) {
                    _parent.GetServerMapping(null, false);
                    ServerInfo rm = _parent._clientConfig.getMappedServer(response.getIp().toString(), response.getServerPort());
                    newServerJoined = rm;
                } else {
                    newServerJoined = new ServerInfo(response.getIp(), response.getServerPort());
                }

                _parent._clientConfig.addServer(newServerJoined);
            }

            clusterAddress = new Address(newServerJoined.getIP(), newServerJoined.getPort());
            serverAddress = new Address(newServerJoined.getIP(), newServerJoined.getPort());
            _parent.setNodeIP(serverAddress.getIpAddress());
            _parent.setNewServerPort(serverAddress.getPort());
            _parent.getCache().getCacheClusterEventsListener().OnMemberJoined(clusterAddress, serverAddress);
            if(_parent.getLogger().getIsDetailedLogsEnabled()) {
                _parent.getLogger().getNCacheLog().Info("Broker.StartBalancingClients",
                        "response.isReconnectClients(): " + response.isReconnectClients() +
                                " ;; _parent._clientConfig.getBalanceNodes(): " + _parent._clientConfig.getBalanceNodes() +
                                " ;; serverAddress.getIpAddress(): " + serverAddress.getIpAddress() +
                                " ;; serverAddress.getPort(): " + serverAddress.getPort());
            }
            if (response.isReconnectClients() && _parent._clientConfig.getBalanceNodes()) {
                boolean isReleaseLock = false;
                try {
                    _parent.getlock().AcquireWriterLock();
                    isReleaseLock = true;

                    _parent.getConnection().getStatusLatch().SetStatusBit((byte) (ConnectionStatus.Connecting | ConnectionStatus.LoadBalance), (byte) (ConnectionStatus.Connected | ConnectionStatus.Disconnected));
                    int totalTimeToWait = _parent.getOperationTimeout();
                    int timeSlice = 2000;
                    tangible.RefObject tempRef_totalTimeToWait = new tangible.RefObject(totalTimeToWait);
                    int nextInterval = _parent.NextWaitInterval(tempRef_totalTimeToWait, timeSlice);
                    totalTimeToWait = tempRef_totalTimeToWait.argvalue;

                    do {
                        if (_parent.getLogger().getIsDetailedLogsEnabled()) {
                            _parent.getLogger().getNCacheLog().Info("Broker.StartBalancingClients", "total wait time remaining: " + (new Integer(totalTimeToWait)).toString());
                            _parent.getLogger().getNCacheLog().Info("Broker.StartBalancingClients", "current wait interval: " + (new Integer(nextInterval)).toString());
                        }

                        Thread.sleep(nextInterval);

                        synchronized (_parent.getRequestTable()) {
                            if (_parent.getRequestTable().size() == 0) {
                                break;
                            }
                            if (_parent.getLogger().getIsDetailedLogsEnabled()) {
                                _parent.getLogger().getNCacheLog().Info("Broker.StartBalancingClients", "Responses remaining: " + _parent.getRequestTable().size());
                            }
                        }

                        tangible.RefObject tempRef_totalTimeToWait2 = new tangible.RefObject(totalTimeToWait);
                        nextInterval = _parent.NextWaitInterval(tempRef_totalTimeToWait2, timeSlice);
                        totalTimeToWait = tempRef_totalTimeToWait2.argvalue;
                    } while (nextInterval > 0);


                    _parent.ResetBroker(_parent.getServerIP());
                    _parent.getConnection().getStatusLatch().SetStatusBit(ConnectionStatus.Connecting, ConnectionStatus.LoadBalance);
                    _parent.getConnection().Disconnect();
                    Thread.sleep(5000);
                    Exception exception = null;
                    tangible.RefObject tempRef_exception = new tangible.RefObject<>(exception);
                    boolean tempVar = !_parent.ConnectRemoteServer(_parent.getConnection(), _parent.getNodeIP(), _parent.getNewServerPort(), false, false, true, tempRef_exception, true);
                    exception = tempRef_exception.argvalue;
                    if (tempVar) {
                        _parent.getlock().ReleaseWriterLock();
                        isReleaseLock = false;
                        _parent.TryNextServer();
                    } else {
                        _parent.getConnection().getStatusLatch().SetStatusBit(ConnectionStatus.Connected, ConnectionStatus.Connecting);
                    }
                } finally {
                    if (isReleaseLock) {
                        _parent.getlock().ReleaseWriterLock();
                    }
                }
            }
        } catch (Exception  e) {
            if (_parent.getLogger().getIsErrorLogsEnabled()) {
                _parent.getLogger().getNCacheLog().Error("Broker.StartBalancingClients", "Error: " + e);
            }
        }

    }

    public void setResponse(CommandResponse response) {
        this.response = response;
    }

    public void setParent(Broker broker) {
        _parent = broker;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy