Search in sources :

Example 1 with TimeoutException

use of co.paralleluniverse.galaxy.TimeoutException in project quasar by puniverse.

the class PeerTKB method run.

public void run() throws ExecutionException, InterruptedException {
    switch(SCENARIO.testGenEvent) {
        case test:
            final Store store = Grid.getInstance().store();
            if (i == 1) {
                StoreTransaction tx = store.beginTransaction();
                try {
                    long root = store.getRoot("root", tx);
                    // "hello".getBytes();
                    byte[] buf = null;
                    store.set(root, buf, tx);
                    store.commit(tx);
                } catch (TimeoutException ex) {
                    throw new RuntimeException("set failed");
                }
                Thread.sleep(20000);
            } else {
                StoreTransaction tx = store.beginTransaction();
                byte[] get;
                try {
                    long root = store.getRoot("root", tx);
                    get = store.get(root);
                    store.commit(tx);
                } catch (TimeoutException ex) {
                    throw new RuntimeException("get failed");
                }
                System.out.println(get);
            }
            break;
        case testGenServer:
            if (i == 1) {
                spawnGenServer(new AbstractServerHandler<Message, Integer, Message>() {

                    @Override
                    public void init() throws SuspendExecution {
                        super.init();
                        ServerActor.currentServerActor().register("myServer");
                    }

                    @Override
                    public Integer handleCall(ActorRef<?> from, Object id, Message m) {
                        return m.a + m.b;
                    }
                }).join();
            } else {
                Integer get = spawnActor(new BasicActor<Message, Integer>(new MailboxConfig(10, Channels.OverflowPolicy.THROW)) {

                    protected Integer doRun() throws SuspendExecution, InterruptedException {
                        final Server<Message, Integer, Message> gs = (Server) ActorRegistry.getActor("myServer");
                        return gs.call(new Message(3, 4));
                    }
                }).get();
                System.out.println("value is " + get);
                assert get == 7;
            }
            break;
        case testGenEvent:
            if (i == 1) {
                final Val<String> dv = new Val<>();
                spawnGenEvent(new Initializer() {

                    @Override
                    public void init() throws SuspendExecution {
                        EventSourceActor.currentEventSourceActor().register("myEventServer");
                        try {
                            final EventSource<String> ge = LocalActor.self();
                            ge.addHandler(new EventHandler<String>() {

                                @Override
                                public void handleEvent(String event) {
                                    dv.set(event);
                                    System.out.println("sout " + event);
                                    ge.shutdown();
                                }
                            });
                        } catch (InterruptedException ex) {
                            System.out.println(ex);
                        }
                    }

                    @Override
                    public void terminate(Throwable cause) throws SuspendExecution {
                        System.out.println("terminated");
                    }
                });
                String get = dv.get();
                System.out.println("got msg " + get);
                assert get.equals("hello world");
            } else {
                spawnActor(new BasicActor<Message, Void>() {

                    protected Void doRun() throws SuspendExecution, InterruptedException {
                        final EventSource<String> ge = (EventSource) ActorRegistry.getActor("myEventServer");
                        ge.notify("hello world");
                        return null;
                    }
                }).join();
            }
            break;
        case testMultiGetActor:
            if (i == 1) {
                spawnGenEvent(new Initializer() {

                    AtomicInteger ai = new AtomicInteger();

                    @Override
                    public void init() throws SuspendExecution {
                        Actor.currentActor().register("myEventServer");
                        try {
                            final EventSource<String> ge = LocalActor.self();
                            ge.addHandler(new EventHandler<String>() {

                                @Override
                                public void handleEvent(String event) {
                                    System.out.println("msg no " + ai.incrementAndGet() + ": " + event);
                                }
                            });
                        } catch (InterruptedException ex) {
                            System.out.println(ex);
                        }
                    }

                    @Override
                    public void terminate(Throwable cause) throws SuspendExecution {
                        System.out.println("terminated");
                    }
                }).join();
            } else {
                Queue<Actor> queue = new LinkedList<>();
                for (int j = 0; j < 1000; j++) {
                    final BasicActor<Message, Void> actor = spawnActor(new BasicActor<Message, Void>("actor-" + j) {

                        protected Void doRun() throws SuspendExecution, InterruptedException {
                            try {
                                final EventSource<String> ge = (EventSource) ActorRegistry.getActor("myEventServer");
                                ge.notify("hwf " + getName());
                            } catch (Exception e) {
                                System.out.println("error in " + getName());
                                throw e;
                            }
                            return null;
                        }
                    });
                    queue.add(actor);
                // actor.join();
                }
                for (Actor localActor : queue) localActor.join();
                Thread.sleep(500);
            }
            break;
        case testOrdering:
            if (i == 1) {
                spawnGenEvent(new Initializer() {

                    AtomicInteger ai = new AtomicInteger();

                    @Override
                    public void init() throws SuspendExecution {
                        EventSourceActor.currentEventSourceActor().register("myEventServer");
                        try {
                            EventSourceActor<String> ge = EventSourceActor.currentEventSourceActor();
                            ge.ref().addHandler(new EventHandler<String>() {

                                @Override
                                public void handleEvent(String event) {
                                    System.out.println("msg no " + ai.incrementAndGet() + ": " + event);
                                }
                            });
                        } catch (InterruptedException ex) {
                            System.out.println(ex);
                        }
                    }

                    @Override
                    public void terminate(Throwable cause) throws SuspendExecution {
                        System.out.println("terminated");
                    }
                }).join();
            } else {
                Queue<Actor> queue = new LinkedList<>();
                for (int j = 0; j < 1; j++) {
                    final BasicActor<Message, Void> actor = spawnActor(new BasicActor<Message, Void>("actor-" + j) {

                        protected Void doRun() throws SuspendExecution, InterruptedException {
                            try {
                                final EventSource<String> ge = (EventSource) ActorRegistry.getActor("myEventServer");
                                for (int k = 0; k < 3000; k++) ge.notify("hw " + k + " f" + getName());
                            } catch (Exception e) {
                                System.out.println("error in " + getName());
                                throw e;
                            }
                            return null;
                        }
                    });
                    queue.add(actor);
                // actor.join();
                }
                for (Actor localActor : queue) localActor.join();
                Thread.sleep(5000);
            }
            break;
        default:
    }
    System.out.println("finished");
    System.exit(0);
    while (true) {
        System.out.println("==================");
        ThreadUtil.dumpThreads();
        Thread.sleep(5000);
    }
}
Also used : Val(co.paralleluniverse.strands.dataflow.Val) Server(co.paralleluniverse.actors.behaviors.Server) StoreTransaction(co.paralleluniverse.galaxy.StoreTransaction) ActorRef(co.paralleluniverse.actors.ActorRef) Store(co.paralleluniverse.galaxy.Store) EventHandler(co.paralleluniverse.actors.behaviors.EventHandler) MailboxConfig(co.paralleluniverse.actors.MailboxConfig) LocalActor(co.paralleluniverse.actors.LocalActor) BasicActor(co.paralleluniverse.actors.BasicActor) EventSourceActor(co.paralleluniverse.actors.behaviors.EventSourceActor) Actor(co.paralleluniverse.actors.Actor) ServerActor(co.paralleluniverse.actors.behaviors.ServerActor) AbstractServerHandler(co.paralleluniverse.actors.behaviors.AbstractServerHandler) TimeoutException(co.paralleluniverse.galaxy.TimeoutException) SuspendExecution(co.paralleluniverse.fibers.SuspendExecution) BasicActor(co.paralleluniverse.actors.BasicActor) LinkedList(java.util.LinkedList) TimeoutException(co.paralleluniverse.galaxy.TimeoutException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) EventSource(co.paralleluniverse.actors.behaviors.EventSource) Initializer(co.paralleluniverse.actors.behaviors.Initializer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 2 with TimeoutException

use of co.paralleluniverse.galaxy.TimeoutException in project quasar by puniverse.

the class GlxGlobalRegistry method register.

@Override
public <Message> void register(Actor<Message, ?> actor, ActorRef<Message> actorRef) throws SuspendExecution {
    final String rootName = actorRef.getName();
    LOG.info("Registering actor {} at root {}", actorRef, rootName);
    final Store store = grid.store();
    StoreTransaction txn = store.beginTransaction();
    lock.lock();
    try {
        try {
            Object globalId = getGlobalId(actor);
            final long root = store.getRoot(rootName, globalId != null ? (Long) globalId : -1, txn);
            // assert globalId == null || ((Long) globalId) == root; -- it's OK to replace the actor's globalId -- until it's too late
            setGlobalId(actor, root);
            store.getx(root, txn);
            store.set(root, Serialization.getInstance().write(actorRef), txn);
            final long version = store.getVersion(root);
            LOG.debug("Registered actor {} at rootId  {}", actorRef, Long.toHexString(root));
            store.commit(txn);
            // <--- comment out to test "distribution" on a single node
            updateCache(rootName, new CacheEntry(actorRef, root, version));
        } catch (TimeoutException e) {
            LOG.error("Registering actor {} at root {} failed due to timeout", actorRef, rootName);
            store.rollback(txn);
            store.abort(txn);
            throw new RuntimeException("Actor registration failed");
        }
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    } finally {
        lock.unlock();
    }
}
Also used : StoreTransaction(co.paralleluniverse.galaxy.StoreTransaction) Store(co.paralleluniverse.galaxy.quasar.Store) TimeoutException(co.paralleluniverse.galaxy.TimeoutException)

Example 3 with TimeoutException

use of co.paralleluniverse.galaxy.TimeoutException in project quasar by puniverse.

the class GlxGlobalRegistry method getOrRegisterActor0.

private CacheEntry getOrRegisterActor0(final String rootName, Callable<? extends ActorRef<?>> actorFactory) throws SuspendExecution, RuntimeException {
    final Store store = grid.store();
    final StoreTransaction txn = store.beginTransaction();
    try {
        try {
            final long root = store.getRoot(rootName, txn);
            final ActorRef<?> actor;
            final byte[] buf = store.getx(root, txn);
            long version = store.getVersion(root);
            if (buf == null || buf.length == 0) {
                try {
                    actor = actorFactory.call();
                } catch (Exception e) {
                    throw new RuntimeException("Exception while creating actor", e);
                }
                LOG.debug("Store returned null for root {}. Registering actor {} at rootId  {}", rootName, actor, root);
                store.set(root, Serialization.getInstance().write(actor), txn);
                version = store.getVersion(root);
            } else
                actor = deserActor(rootName, buf);
            store.commit(txn);
            return new CacheEntry(actor, root, version);
        } catch (TimeoutException e) {
            LOG.error("Getting actor {} failed due to timeout", rootName);
            store.rollback(txn);
            store.abort(txn);
            throw new RuntimeException("Actor discovery/registration failed");
        }
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
}
Also used : StoreTransaction(co.paralleluniverse.galaxy.StoreTransaction) Store(co.paralleluniverse.galaxy.quasar.Store) TimeoutException(co.paralleluniverse.galaxy.TimeoutException) TimeoutException(co.paralleluniverse.galaxy.TimeoutException)

Example 4 with TimeoutException

use of co.paralleluniverse.galaxy.TimeoutException in project quasar by puniverse.

the class GlxGlobalRegistry method getActor0.

private CacheEntry getActor0(final String rootName, long timeout, TimeUnit unit) throws SuspendExecution, RuntimeException, InterruptedException {
    final long deadline = unit != null ? System.nanoTime() + unit.toNanos(timeout) : 0;
    final Store store = grid.store();
    final long root;
    final ReentrantLock lck0 = new ReentrantLock();
    final Condition cond = lck0.newCondition();
    boolean listening = false;
    final StoreTransaction txn = store.beginTransaction();
    try {
        root = store.getRoot(rootName, txn);
        final CacheListener listener = new AbstractCacheListener() {

            @Override
            public void evicted(Cache cache, long id) {
                invalidated(cache, id);
            }

            @Override
            public void invalidated(Cache cache, long id) {
                grid.getDelegate().store().getAsync(id);
            }

            @Override
            public void received(Cache cache, long id, long version, ByteBuffer data) {
                if (data != null && data.remaining() > 0) {
                    LOG.debug("Received root {} ({})", rootName, Long.toHexString(id));
                    lck0.lock();
                    try {
                        cond.signalAll();
                    } finally {
                        lck0.unlock();
                    }
                    store.setListener(root, null);
                }
            }
        };
        listening = (store.setListenerIfAbsent(root, listener) == listener);
        store.commit(txn);
    } catch (TimeoutException e) {
        LOG.error("Getting actor {} failed due to timeout", rootName);
        store.rollback(txn);
        store.abort(txn);
        throw new RuntimeException("Actor discovery failed");
    }
    try {
        byte[] buf = store.gets(root, null);
        long version = store.getVersion(root);
        store.release(root);
        if (listening) {
            lck0.lock();
            try {
                while (buf == null || buf.length == 0) {
                    LOG.debug("Store returned null for root {}", rootName);
                    if (deadline > 0) {
                        final long now = System.nanoTime();
                        if (now > deadline)
                            // throw new java.util.concurrent.TimeoutException();
                            return null;
                        cond.await(deadline - now, TimeUnit.NANOSECONDS);
                    } else
                        cond.await();
                    buf = store.gets(root, null);
                    version = store.getVersion(root);
                    store.release(root);
                }
            } finally {
                lck0.unlock();
            }
        } else
            assert buf != null && buf.length > 0;
        final ActorRef<?> actor = deserActor(rootName, buf);
        return new CacheEntry(actor, root, version);
    } catch (TimeoutException e) {
        LOG.error("Getting actor {} failed due to timeout", rootName);
        throw new RuntimeException("Actor discovery failed");
    }
}
Also used : ReentrantLock(co.paralleluniverse.strands.concurrent.ReentrantLock) Condition(java.util.concurrent.locks.Condition) StoreTransaction(co.paralleluniverse.galaxy.StoreTransaction) Store(co.paralleluniverse.galaxy.quasar.Store) ByteBuffer(java.nio.ByteBuffer) CacheListener(co.paralleluniverse.galaxy.CacheListener) AbstractCacheListener(co.paralleluniverse.galaxy.AbstractCacheListener) AbstractCacheListener(co.paralleluniverse.galaxy.AbstractCacheListener) Cache(co.paralleluniverse.galaxy.Cache) TimeoutException(co.paralleluniverse.galaxy.TimeoutException)

Example 5 with TimeoutException

use of co.paralleluniverse.galaxy.TimeoutException in project quasar by puniverse.

the class GlxGlobalRegistry method unregister0.

private void unregister0(ActorRef<?> actor) throws SuspendExecution {
    final String rootName = actor.getName();
    LOG.info("Unregistering {}", rootName);
    final Store store = grid.store();
    StoreTransaction txn = store.beginTransaction();
    try {
        try {
            final long root = store.getRoot(rootName, txn);
            store.set(root, (byte[]) null, txn);
            store.commit(txn);
        } catch (TimeoutException e) {
            LOG.error("Unregistering {} failed due to timeout", rootName);
            store.rollback(txn);
            store.abort(txn);
            throw new RuntimeException("Actor unregistration failed");
        }
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
}
Also used : StoreTransaction(co.paralleluniverse.galaxy.StoreTransaction) Store(co.paralleluniverse.galaxy.quasar.Store) TimeoutException(co.paralleluniverse.galaxy.TimeoutException)

Aggregations

TimeoutException (co.paralleluniverse.galaxy.TimeoutException)7 StoreTransaction (co.paralleluniverse.galaxy.StoreTransaction)6 Store (co.paralleluniverse.galaxy.quasar.Store)5 Actor (co.paralleluniverse.actors.Actor)1 ActorRef (co.paralleluniverse.actors.ActorRef)1 BasicActor (co.paralleluniverse.actors.BasicActor)1 LocalActor (co.paralleluniverse.actors.LocalActor)1 MailboxConfig (co.paralleluniverse.actors.MailboxConfig)1 AbstractServerHandler (co.paralleluniverse.actors.behaviors.AbstractServerHandler)1 EventHandler (co.paralleluniverse.actors.behaviors.EventHandler)1 EventSource (co.paralleluniverse.actors.behaviors.EventSource)1 EventSourceActor (co.paralleluniverse.actors.behaviors.EventSourceActor)1 Initializer (co.paralleluniverse.actors.behaviors.Initializer)1 Server (co.paralleluniverse.actors.behaviors.Server)1 ServerActor (co.paralleluniverse.actors.behaviors.ServerActor)1 Streamable (co.paralleluniverse.common.io.Streamable)1 SuspendExecution (co.paralleluniverse.fibers.SuspendExecution)1 AbstractCacheListener (co.paralleluniverse.galaxy.AbstractCacheListener)1 Cache (co.paralleluniverse.galaxy.Cache)1 CacheListener (co.paralleluniverse.galaxy.CacheListener)1