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

org.nustaq.kontraktor.services.datacluster.DataShard Maven / Gradle / Ivy

There is a newer version: 5.2.0
Show newest version
package org.nustaq.kontraktor.services.datacluster;

import org.nustaq.kontraktor.Actors;
import org.nustaq.kontraktor.IPromise;
import org.nustaq.kontraktor.Promise;
import org.nustaq.kontraktor.remoting.base.ConnectableActor;
import org.nustaq.kontraktor.remoting.tcp.TCPConnectable;
import org.nustaq.kontraktor.services.ServiceActor;
import org.nustaq.kontraktor.services.ServiceArgs;
import org.nustaq.kontraktor.remoting.base.ServiceDescription;
import org.nustaq.kontraktor.services.ServiceRegistry;
import org.nustaq.kontraktor.util.Log;
import org.nustaq.reallive.server.actors.TableSpaceActor;

import java.io.File;
import java.io.Serializable;

/**
 * Created by ruedi on 15.08.2015.
 * Runs serverside, accessed as remote actor in client
 */
public class DataShard extends ServiceActor {

    public static final String DATA_SHARD_NAME = "DataShard";
    TableSpaceActor tableSpace;

    @Override
    public IPromise init(ConnectableActor registryConnectable, ServiceArgs options, boolean auto /*ignored*/) {
        IPromise p = new Promise();
        try {
            super.init(registryConnectable, options,false).then( (r,e) -> {
                try {
                    initTableSpace();
                    registerSelf();
                    p.resolve();
                } catch (Exception ex) {
                    Log.Error(this,ex);
                }
            });
        } catch (Throwable t) {
            p.reject(t);
        }
        return p;
    }

    protected int getPort() {
        return cmdline.getDataShardPortBase()+getCmdline().getShardNo();
    }

    protected void initTableSpace() {
        if ( config == null )
        {
            Log.Error(this,"no registry config received or failed to connect Service Registry");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.exit(1);
        }
        DataCfg dataCfg = config.getDataCluster();
        tableSpace = Actors.AsActor( TableSpaceActor.class );
        String dir = dataCfg.getDataDir()[getCmdline().getShardNo()];
        new File(dir).mkdirs();
        tableSpace.setBaseDataDir(dir);
        tableSpace.init();
        tableSpace.ping().await();
        Log.Info(this, "init tablespace in "+
            dataCfg.getDataDir()[getCmdline().getShardNo()]+
            " shard "+ getCmdline().getShardNo()+" of "+dataCfg.getNumberOfShards()
        );
    }

    public IPromise getTableSpace() {
        return resolve(tableSpace);
    }

    @Override
    protected boolean isFixedDataCluster() {
        return false;
    }

    @Override
    protected String[] getRequiredServiceNames() {
        return new String[0];
    }

    @Override
    protected ServiceDescription createServiceDescription() {
        return new ServiceDescription(DATA_SHARD_NAME + getCmdline().getShardNo())
                .connectable( new TCPConnectable(DataShard.class, cmdline.getHost(), cmdline.getDataShardPortBase()+ getCmdline().getShardNo()));
    }

    protected DataShardArgs getCmdline() {
        return (DataShardArgs) cmdline;
    }

    @Override
    protected Serializable getStatus() {
        return "{ \"connections\":"+(__connections != null ? __connections.size() : 0)+"}";
    }

    public static void main(String[] args) {
        start(args);
    }

    public static DataShard start(String[] args) {
        DataShardArgs options = (DataShardArgs) ServiceRegistry.parseCommandLine(args, null, DataShardArgs.New());
        return start(options);
    }

    public static DataShard start(DataShardArgs options) {
        DataShard ds = Actors.AsActor(DataShard.class,256000);
        ds.init(new TCPConnectable(ServiceRegistry.class, options.getRegistryHost(), options.getRegistryPort()), options, true); // .await(); fail ..
        Log.Info(ds.getClass(), "Init finished");
        return ds;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy