Search in sources :

Example 21 with IPromise

use of org.nustaq.kontraktor.IPromise in project kontraktor by RuedigerMoeller.

the class EmbeddedRealLive method createTable.

/**
 * WARNING: never create more than one table using the same file. This will
 * result in corrupted data for sure. As actor refs (tables) are thread save,
 * just init a singleton containing all your tables once.
 *
 * @param desc
 * @param dataDir - if null use path from description
 * @return a thread save actor reference to a newly loaded or created table
 */
public IPromise<RealLiveTable> createTable(TableDescription desc, String dataDir) {
    RealLiveTableActor table = Actors.AsActor(RealLiveTableActor.class);
    Supplier<RecordStorage> memFactory;
    if (desc.getFilePath() == null) {
        Log.Info(this, "no file specified. all data in memory " + desc.getName());
        switch(desc.getStorageType()) {
            case CACHED:
                memFactory = () -> new CachedOffHeapStorage(new OffHeapRecordStorage(desc.getKeyLen(), desc.getSizeMB(), desc.getNumEntries()), new HeapRecordStorage());
                break;
            default:
                Log.Error(this, "unknown storage type " + desc.getStorageType() + " default to PERSIST");
            case PERSIST:
                memFactory = () -> new OffHeapRecordStorage(desc.getKeyLen(), desc.getSizeMB(), desc.getNumEntries());
                break;
            case TEMP:
                memFactory = () -> new HeapRecordStorage();
                break;
        }
    } else {
        String bp = dataDir == null ? desc.getFilePath() : dataDir;
        desc.filePath(bp);
        new File(bp).mkdirs();
        String file = bp + "/" + desc.getName() + "_" + desc.getShardNo() + ".bin";
        switch(desc.getStorageType()) {
            case CACHED:
                Log.Info(this, "memory mapping file " + file);
                memFactory = () -> new CachedOffHeapStorage(new OffHeapRecordStorage(file, desc.getKeyLen(), desc.getSizeMB(), desc.getNumEntries()), new HeapRecordStorage());
                break;
            default:
                Log.Error(this, "unknown storage type " + desc.getStorageType() + " default to PERSIST");
            case PERSIST:
                Log.Info(this, "memory mapping file " + file);
                memFactory = () -> new OffHeapRecordStorage(file, desc.getKeyLen(), desc.getSizeMB(), desc.getNumEntries());
                break;
            case TEMP:
                memFactory = () -> new HeapRecordStorage();
                break;
        }
    }
    table.init(memFactory, desc).await(30_000);
    return new Promise(table);
}
Also used : Promise(org.nustaq.kontraktor.Promise) IPromise(org.nustaq.kontraktor.IPromise) OffHeapRecordStorage(org.nustaq.reallive.impl.storage.OffHeapRecordStorage) HeapRecordStorage(org.nustaq.reallive.impl.storage.HeapRecordStorage) OffHeapRecordStorage(org.nustaq.reallive.impl.storage.OffHeapRecordStorage) RecordStorage(org.nustaq.reallive.api.RecordStorage) HeapRecordStorage(org.nustaq.reallive.impl.storage.HeapRecordStorage) File(java.io.File) CachedOffHeapStorage(org.nustaq.reallive.impl.storage.CachedOffHeapStorage) OffHeapRecordStorage(org.nustaq.reallive.impl.storage.OffHeapRecordStorage) RealLiveTableActor(org.nustaq.reallive.impl.actors.RealLiveTableActor)

Example 22 with IPromise

use of org.nustaq.kontraktor.IPromise in project kontraktor by RuedigerMoeller.

the class StorageDriver method atomicQuery.

/**
 * apply the function to the record with given key and return the result inside a promise
 *
 * changes to the record inside the function are applied to the real record and a change message
 * is generated.
 *
 * In case the function returns a changemessage (add,putRecord,remove ..), the change message is applied
 * to the original record and the change is broadcasted
 *
 * @param key
 * @param action
 * @return the result of function.
 */
public IPromise atomicQuery(String key, RLFunction<Record, Object> action) {
    Record rec = getStore().get(key);
    if (rec == null) {
        final Object apply = action.apply(rec);
        if (apply instanceof ChangeMessage) {
            receive((ChangeMessage) apply);
        }
        return new Promise(apply);
    } else {
        PatchingRecord pr = new PatchingRecord(rec);
        final Object res = action.apply(pr);
        if (res instanceof ChangeMessage) {
            receive((ChangeMessage) res);
        } else {
            UpdateMessage updates = pr.getUpdates();
            if (updates != null) {
                receive(updates);
            }
        }
        return new Promise(res);
    }
}
Also used : IPromise(org.nustaq.kontraktor.IPromise) Promise(org.nustaq.kontraktor.Promise) PatchingRecord(org.nustaq.reallive.records.PatchingRecord) PatchingRecord(org.nustaq.reallive.records.PatchingRecord)

Example 23 with IPromise

use of org.nustaq.kontraktor.IPromise in project kontraktor by RuedigerMoeller.

the class ServiceActor method awaitRequiredServices.

public IPromise awaitRequiredServices() {
    Log.Info(this, "connecting required services ..");
    if (requiredServices.size() == 0) {
        return resolve();
    }
    IPromise res = new Promise<>();
    gravity.getServiceMap().then((smap, err) -> {
        List<IPromise<Object>> servicePromis = new ArrayList();
        String[] servNames = getAllServiceNames();
        for (int i = 0; i < servNames.length; i++) {
            String servName = servNames[i];
            ServiceDescription serviceDescription = smap.get(servName);
            if (serviceDescription != null && requiredServices.get(servName) instanceof Actor == false) {
                if (serviceDescription.getConnectable() == null) {
                    Log.Error(this, "No connecteable defined for service " + serviceDescription.getName());
                }
                IPromise connect;
                try {
                    Log.Info(this, "connect " + serviceDescription.getConnectable());
                    connect = serviceDescription.getConnectable().connect();
                } catch (Throwable th) {
                    Log.Error(this, th, "failed to connect " + serviceDescription.getName());
                    continue;
                }
                Promise notify = new Promise();
                servicePromis.add(notify);
                connect.then((actor, connectionError) -> {
                    if (actor != null) {
                        requiredServices.put(servName, actor);
                        notify.complete();
                    } else {
                        requiredServices.put(servName, UNCONNECTED);
                        Log.Warn(this, "failed to connect " + servName + " " + connectionError + " " + serviceDescription.getConnectable());
                        notify.reject("failed to connect " + servName + " " + connectionError);
                    }
                });
            } else {
                res.reject("required service " + servName + " not registered.");
                return;
            }
        }
        if (!res.isSettled()) {
            all(servicePromis).timeoutIn(15000).then(res).onTimeout(() -> {
                // todo:retry
                Log.Info(this, "failed to connect required services, retry");
            });
        }
    });
    return res;
}
Also used : Promise(org.nustaq.kontraktor.Promise) IPromise(org.nustaq.kontraktor.IPromise) IPromise(org.nustaq.kontraktor.IPromise) TableSpaceActor(org.nustaq.reallive.impl.tablespace.TableSpaceActor) ConnectableActor(org.nustaq.kontraktor.remoting.base.ConnectableActor) Actor(org.nustaq.kontraktor.Actor)

Example 24 with IPromise

use of org.nustaq.kontraktor.IPromise in project kontraktor by RuedigerMoeller.

the class DataClient method export.

/**
 * @param directory
 */
public IPromise export(String directory) {
    Promise res = new Promise();
    // use separate thread to enable slowly, blocking processing
    Actors.exec.execute(() -> {
        File d = new File(directory);
        if (d.exists() && (!d.isDirectory() || !d.canWrite())) {
            res.reject(new RuntimeException("cannot write to " + d + " or not a directory"));
            return;
        } else {
            d.mkdirs();
        }
        FSTConfiguration writeConf = FSTConfiguration.createDefaultConfiguration();
        Arrays.stream(config.getSchema()).forEach(desc -> {
            try {
                DataOutputStream fout = new DataOutputStream(new FileOutputStream(new File(d, desc.getName() + ".oos")));
                CountDownLatch pl = new CountDownLatch(shards.length);
                for (int i = 0; i < shards.length; i++) {
                    TableSpaceActor shard = shards[i];
                    Log.Info(this, "exporting shard " + i + " table " + desc.getName());
                    try {
                        RealLiveTable table = shard.getTableAsync(desc.getName()).await(60_000);
                        table.forEach(rec -> true, (rec, err) -> {
                            if (rec != null) {
                                try {
                                    // write marker to enable recovery in case of corruption
                                    synchronized (fout) {
                                        fout.write(31);
                                        fout.write(32);
                                        fout.write(33);
                                        fout.write(34);
                                        byte[] b = writeConf.asByteArray(rec);
                                        fout.writeInt(b.length);
                                        fout.write(b);
                                    }
                                } catch (IOException e) {
                                    Log.Error(this, e);
                                }
                            } else if (err != null) {
                                Log.Warn(this, "error during export " + err);
                                pl.countDown();
                            } else {
                                // fin
                                pl.countDown();
                            }
                        });
                    } catch (Exception e) {
                        Log.Error(this, "export failure " + desc.getName() + " shard " + i);
                    }
                }
                try {
                    boolean succ = pl.await(5, TimeUnit.MINUTES);
                    if (!succ)
                        Log.Error(this, "export timed out on table " + desc.getName());
                    try {
                        fout.close();
                    } catch (IOException e) {
                        Log.Error(this, e);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } catch (FileNotFoundException e) {
                Log.Error(this, e);
            }
        });
        res.complete();
    });
    return res;
}
Also used : RealLiveTable(org.nustaq.reallive.api.RealLiveTable) TableSpaceActor(org.nustaq.reallive.impl.tablespace.TableSpaceActor) CountDownLatch(java.util.concurrent.CountDownLatch) Promise(org.nustaq.kontraktor.Promise) IPromise(org.nustaq.kontraktor.IPromise) FSTConfiguration(org.nustaq.serialization.FSTConfiguration)

Example 25 with IPromise

use of org.nustaq.kontraktor.IPromise in project kontraktor by RuedigerMoeller.

the class DataClient method connect.

public IPromise connect(DataCfg config, TableSpaceActor[] shards, ServiceActor hostingService) {
    this.config = config;
    this.hostingService = hostingService;
    this.shards = shards;
    syncTableAccess = new HashMap();
    tableSharding = new TableSpaceSharding(shards, key -> Math.abs(key.hashCode()) % shards.length);
    tableSharding.init().await();
    TableDescription[] schema = config.getSchema();
    return all(schema.length, i -> {
        Promise p = new Promise();
        tableSharding.createOrLoadTable(schema[i]).then((r, e) -> {
            if (r != null) {
                syncTableAccess.put(schema[i].getName(), r);
            }
            p.complete(r, e);
        });
        return p;
    });
}
Also used : Record(org.nustaq.reallive.api.Record) Arrays(java.util.Arrays) TableSpaceActor(org.nustaq.reallive.impl.tablespace.TableSpaceActor) Promise(org.nustaq.kontraktor.Promise) HashMap(java.util.HashMap) RealLiveTable(org.nustaq.reallive.api.RealLiveTable) Callback(org.nustaq.kontraktor.Callback) TimeUnit(java.util.concurrent.TimeUnit) TableDescription(org.nustaq.reallive.api.TableDescription) CountDownLatch(java.util.concurrent.CountDownLatch) IPromise(org.nustaq.kontraktor.IPromise) java.io(java.io) TableSpaceSharding(org.nustaq.reallive.impl.tablespace.TableSpaceSharding) ServiceActor(org.nustaq.kontraktor.services.ServiceActor) CallerSideMethod(org.nustaq.kontraktor.annotations.CallerSideMethod) Log(org.nustaq.kontraktor.util.Log) FSTConfiguration(org.nustaq.serialization.FSTConfiguration) Actors(org.nustaq.kontraktor.Actors) RLPredicate(org.nustaq.reallive.api.RLPredicate) ClusteredTableSpaceClient(org.nustaq.reallive.impl.tablespace.ClusteredTableSpaceClient) Promise(org.nustaq.kontraktor.Promise) IPromise(org.nustaq.kontraktor.IPromise) HashMap(java.util.HashMap) TableSpaceSharding(org.nustaq.reallive.impl.tablespace.TableSpaceSharding) TableDescription(org.nustaq.reallive.api.TableDescription)

Aggregations

IPromise (org.nustaq.kontraktor.IPromise)58 Promise (org.nustaq.kontraktor.Promise)56 Remoted (org.nustaq.kontraktor.annotations.Remoted)6 Actor (org.nustaq.kontraktor.Actor)5 JsonObject (com.eclipsesource.json.JsonObject)4 IOException (java.io.IOException)4 Test (org.junit.Test)4 ByteBuffer (java.nio.ByteBuffer)3 KeyManagementException (java.security.KeyManagementException)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 CertificateException (java.security.cert.CertificateException)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 Consumer (java.util.function.Consumer)3 TableSpaceActor (org.nustaq.reallive.impl.tablespace.TableSpaceActor)3 JsonValue (com.eclipsesource.json.JsonValue)2 ArrayList (java.util.ArrayList)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 ArchiveException (org.apache.commons.compress.archivers.ArchiveException)2 HttpPost (org.apache.http.client.methods.HttpPost)2 StringEntity (org.apache.http.entity.StringEntity)2