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

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

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

import Alachisoft.NCache.Common.DataStructures.NewHashmap;
import Alachisoft.NCache.Common.Net.Address;
import Alachisoft.NCache.Common.Threading.Monitor;

import com.alachisoft.ncache.client.ServerInfo;
import com.alachisoft.ncache.runtime.exceptions.InternalCommandException;
import com.alachisoft.ncache.runtime.exceptions.LicensingException;

import java.net.InetAddress;

class UpdateHashmapAsync implements Runnable {
    private final Broker _broker;
    private byte[] serializedMap;
    private NewHashmap _newHashMap;

    public UpdateHashmapAsync(Broker broker) {
        _broker= broker;
    }


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

    public void setData(byte[] value) {
        this.serializedMap = value;
    }


    public void sethashmap(NewHashmap newHashmap) {
        this._newHashMap=newHashmap;
    }

    private void updateHashmap() {
        byte[] serializedMap = this.serializedMap;
        try {
            if (serializedMap != null) {
                _newHashMap = NewHashmap.DeserializeString(serializedMap, _broker.getCache().getName());
            }
            if(_newHashMap!=null)
            {
                NewHashmapRecieved(_newHashMap);
            }
        } catch (Exception e) {
            if (_broker.getLogger().getIsErrorLogsEnabled()) {
                _broker.getLogger().getNCacheLog().Error("Broker.UpdateHashmapAsync", "An error occurred while installing updated hashmap. Error :" + e.toString());
            }
        }
    }

    private void NewHashmapRecieved(NewHashmap newHashmap) {
        if (newHashmap == null) {
            if (_broker.getLogger().getIsErrorLogsEnabled()) {
                _broker.getLogger().getNCacheLog().CriticalInfo("Broker.NewHashmapReceived", "Hashmap is null... returning");
            }
            return;
        }

        try {
            //we are acquring this lock first to avoid deadlock caused during connection establishment with new servers
            _broker.getlock().AcquireWriterLock();

            synchronized (_broker.getHashmapUpdateMutex()) {
                try {
                    if (_broker.getLogger().getIsDetailedLogsEnabled()) {
                        _broker.getLogger().getNCacheLog().Debug("Broker.NewHashmapReceived", "Hashmap " + newHashmap.toString());
                    }

                    long oldId = _broker.getPool() != null ? _broker.getPool().getLastViewId() : -2;

                    if (newHashmap.getLastViewId() == _broker.getPool().getLastViewId()) {
                        if (newHashmap.isForcefulUpdate()) {
                            if (_broker.getLogger() != null && _broker.getLogger().getNCacheLog() != null) {
                                _broker.getLogger().getNCacheLog().CriticalInfo("Broker.NewHashmapReceived", "Hashmap is same as current pool. Pool " + _broker.getPool().toString() + " New Hashmap " + newHashmap.toString() + " ... is being installed forcefully");
                            }

                        } else {
                            if (_broker.getLogger() != null && _broker.getLogger().getNCacheLog() != null) {
                                _broker.getLogger().getNCacheLog().CriticalInfo("Broker.NewHashmapReceived", "Hashmap is same as current pool. Pool " + _broker.getPool().toString() + " New Hashmap " + newHashmap.toString() + " ... returning");
                            }
                            return;
                        }
                    }

                    if (_broker.getClientConfig().getIPMappingConfigured()) {
                        _broker.GetServerMapping(null, false);
                    }
                    for (int i = 0; i < newHashmap.getMembers().size(); i++) {
                        String ip = (String) newHashmap.getMembers().get(i);
                        int serverPort = _broker.getPort();

                        //+MOiz: Azure remote client task
                        if (_broker.getClientConfig().getIPMappingConfigured()) {
                            ServerInfo server = _broker.getClientConfig().getMappedServer(ip, serverPort);
                            ip = server.getName();
                            serverPort = server.getPort();
                            newHashmap.getMembers().add(i, ip + ":" + (Integer.valueOf(serverPort)).toString());
                        }
                        //-MOiz

                        Address addr = new Address(ip, serverPort);

                        if (!_broker.getPool().Contains(addr)) {
                            InetAddress address = null;
                            Connection connection = null;
                            try {
                                address = InetAddress.getByName(ip);

                                connection = new Connection(_broker);

                                Exception exception = null;

                                tangible.RefObject tempRef_exception = new tangible.RefObject<>(exception);
                                boolean tempVar3 = _broker.ConnectRemoteServer(connection, address, serverPort, false, false, false, tempRef_exception, true);
                                exception = tempRef_exception.argvalue;
                                if (tempVar3) {
                                    _broker.getPool().Add(addr, connection);
                                    _broker._clientConfig.addServer(new ServerInfo(address, serverPort));
                                    if (_broker.getLogger().getIsDetailedLogsEnabled()) {
                                        _broker.getLogger().getNCacheLog().Debug("Broker.NewHashmapRecieved", "Connection made to " + ip + ", and added to pool");
                                    }
                                } else {
                                    if (exception != null && _broker.getLogger().getIsErrorLogsEnabled()) {
                                        _broker.getLogger().getNCacheLog().Error("Broker.NewHashmapRecieved", "Could not connect to " + ip + ". " + exception.toString());
                                    }
                                }
                            } catch (SecurityException sxc) {
                                if (_broker.getLogger().getIsErrorLogsEnabled()) {
                                    _broker.getLogger().getNCacheLog().Error("Broker.NewHashmapRecieved", "You do not have permissions to perform the operation on " + ip + ". " + sxc.toString());
                                }
                            }catch (InternalCommandException ex) {

                                if (_broker.getLogger().getIsErrorLogsEnabled()) {
                                    _broker.getLogger().getNCacheLog().Error("Broker.NewHashmapRecieved", ex.toString());
                                }
                            }
                            catch (LicensingException ex) {

                                if (_broker.getLogger().getIsErrorLogsEnabled()) {
                                    _broker.getLogger().getNCacheLog().Error("Broker.NewHashmapRecieved", ex.toString());
                                }
                            } catch (Exception exc) {
                                _broker.getPool().Add(addr, connection);
                                _broker._clientConfig.addServer(new ServerInfo(address, serverPort));
                                if (_broker.getLogger().getIsErrorLogsEnabled()) {
                                    _broker.getLogger().getNCacheLog().Error("Broker.NewHashmapRecieved", exc.toString());
                                }
                            }
                        } else {
                            Connection connection = _broker.getPool().getItem(addr);

                            if (connection != null && !connection.getIsConnected()) {
                                if (_broker.getLogger().getIsDetailedLogsEnabled()) {
                                    _broker.getLogger().getNCacheLog().Debug("Broker.NewHashmapRecieved", "Not connected to " + ip + " in the pool");
                                }
                                try {
                                    Exception exception = null;
                                    tangible.RefObject tempRef_exception2 = new tangible.RefObject(exception);
                                    _broker.TryConnecting(connection, tempRef_exception2, 0, true, false);
                                    exception = tempRef_exception2.argvalue;
                                } catch (Exception exc) {
                                    if (_broker.getLogger().getIsErrorLogsEnabled()) {
                                        _broker.getLogger().getNCacheLog().Error("Broker.NewHashmapRecieved", exc.toString());
                                    }
                                }
                            } else {
                                if (_broker.getLogger().getIsDetailedLogsEnabled()) {
                                    _broker.getLogger().getNCacheLog().Debug("Broker.NewHashmapRecieved", "Already connected to " + ip + " in the pool");
                                }
                            }
                        }
                    }

                    if (_broker.getImportHashmap()) {
                        //List of Connection ip's which as per new hashmap are no more valid
                        java.util.ArrayList
invalidIPConnection = new java.util.ArrayList
(); for (Address ipAddress : _broker.getPool().getServers()) { if (_broker._clientConfig.getIPMappingConfigured()) { if (!newHashmap.getMembers().contains(ipAddress.getIpAddress().getHostAddress() + ":" + ipAddress.getPort())) { invalidIPConnection.add(ipAddress); } } else { if (!newHashmap.getMembers().contains(ipAddress.getIpAddress().getHostAddress())) { invalidIPConnection.add(ipAddress); } } } for (Address ip : invalidIPConnection) { _broker.getPool().getItem(ip).Disconnect(); _broker.getPool().Remove(ip); Connection tempConnection = _broker.getConnection(); if (tempConnection != null && tempConnection.getAddress().equals(ip.getIpAddress())) { //Let's reset default connection from existing pool _broker.setConnection(_broker.getPool().GetAnyConnection()); } if (_broker.getShutdownServers().containsKey(ip)) { ShutDownServerInfo ssInfo = (ShutDownServerInfo) _broker.getShutdownServers().get(ip); synchronized (ssInfo.getWaitForBlockedActivity()) { Monitor.pulse(ssInfo.getWaitForBlockedActivity()); _broker.getShutdownServers().remove(ip); } } if (_broker.getLogger().getIsDetailedLogsEnabled()) { _broker.getLogger().getNCacheLog().Debug("Broker.NewHashmapRecieved", "Disconnected from " + ip + ", and removed from pool"); } } } ServerInfo srvr = new ServerInfo(); String add = null; java.util.HashMap map = newHashmap.getMap(); for (int key = 0; key < map.size(); key++) { add = (String) map.get(key); srvr = _broker._clientConfig.getMappedServer(add, _broker.getPort()); map.put(key, new Address(InetAddress.getByName(srvr.getName()), srvr.getPort())); } _broker.getPool().SetHashmap(newHashmap); if (_broker.getLogger().getIsDetailedLogsEnabled()) { _broker.getLogger().getNCacheLog().Debug("Broker.NewHashmapReceived", "Hashmap applied " + newHashmap.toString() + " Pool " + _broker.getPool().toString()); } } catch (Exception exc) { if (_broker.getLogger().getIsErrorLogsEnabled()) { _broker.getLogger().getNCacheLog().Error("Broker.NewHashmapRecieved", exc.getMessage()); } } } } finally { _broker.getlock().ReleaseWriterLock(); } _broker.getHashMapStatus().SetStatusBit(HashMapStatus.INITIALIZE, HashMapStatus.UNINITIALIZE); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy