Package co.paralleluniverse.galaxy.quasar

Examples of co.paralleluniverse.galaxy.quasar.Store


    public Object register(ActorRef<?> actor) throws SuspendExecution {
        final String rootName = actor.getName();

        LOG.info("Registering actor {} at root {}", actor, rootName);

        final Store store = grid.store();
        StoreTransaction txn = store.beginTransaction();
        serlock.lock();
        try {
            try {
                final long root = store.getRoot(rootName, txn);
                store.getx(root, txn);
                store.set(root, ser.write(actor), txn);
                LOG.debug("commit Registering actor {} at rootId  {}", actor, root);
                store.commit(txn);
                RemoteChannelReceiver.getReceiver(LocalActor.getMailbox(actor), true).handleRefMessage(new GlxRemoteChannel.RefMessage(true, grid.cluster().getMyNodeId()));
                return root; // root is the global id
            } catch (TimeoutException e) {
                LOG.error("Registering actor {} at root {} failed due to timeout", actor, rootName);
                store.rollback(txn);
                store.abort(txn);
                throw new RuntimeException("Actor registration failed");
            }
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally {
View Full Code Here


    public void unregister(ActorRef<?> actor) throws SuspendExecution {
        final String rootName = actor.getName();

        LOG.info("Uregistering {}", 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);
                RemoteChannelReceiver.getReceiver(LocalActor.getMailbox(actor), true).handleRefMessage(new GlxRemoteChannel.RefMessage(false, grid.cluster().getMyNodeId()));
            } 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);
        }
View Full Code Here

            serlock.unlock();
        }
    }

    private <Message> ActorRef<Message> getRootFromStoreAndUpdateCache(final String rootName) throws SuspendExecution, RuntimeException {
        final Store store = grid.store();

        StoreTransaction txn = store.beginTransaction();
        try {
            boolean error = false;
            try {
                final long root = store.getRoot(rootName, txn);
                byte[] buf = store.get(root);
                if (buf == null)
                    return null;

                if (buf.length == 0)
                    return null; // TODO: Galaxy should return null

                final ActorRef<Message> actor;
                try {
                    actor = (ActorRef<Message>) ser.read(buf);
                } catch (Exception e) {
                    LOG.info("Deserializing actor at root " + rootName + " has failed with exception", e);
                    return null;
                }
                store.setListener(root, new CacheListener() {
                    @Override
                    public void invalidated(Cache cache, long id) {
                        evicted(cache, id);
                    }

                    @Override
                    public void received(Cache cache, long id, long version, ByteBuffer data) {
                        evicted(cache, id);
                    }

                    @Override
                    public void evicted(Cache cache, long id) {
                        rootCache.remove(rootName);
                        store.setListener(id, null);
                    }
                });
                rootCache.put(rootName, actor);
                return actor;
            } catch (TimeoutException e) {
                error = true;
                LOG.error("Getting actor {} failed due to timeout", rootName);
                store.rollback(txn);
                store.abort(txn);
                throw new RuntimeException("Actor discovery failed");
            } finally {
                if(!error)
                    store.commit(txn);
            }

        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
View Full Code Here

    public Object register(ActorRef<?> actor, Object globalId) throws SuspendExecution {
        final String rootName = actor.getName();

        LOG.info("Registering actor {} at root {}", actor, rootName);

        final Store store = grid.store();
        StoreTransaction txn = store.beginTransaction();
        lock.lock();
        try {
            try {
                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
                store.getx(root, txn);
                store.set(root, Serialization.getInstance().write(actor), txn);
                LOG.debug("Registered actor {} at rootId  {}", actor, Long.toHexString(root));
                store.commit(txn);
                return root; // root is the global id
            } catch (TimeoutException e) {
                LOG.error("Registering actor {} at root {} failed due to timeout", actor, rootName);
                store.rollback(txn);
                store.abort(txn);
                throw new RuntimeException("Actor registration failed");
            }
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally {
View Full Code Here

    private void unregister0(ActorRef<?> actor) throws SuspendExecution {
        final String rootName = actor.getName();

        LOG.info("Uregistering {}", 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);
        }
View Full Code Here

            return cacheValue;
        return getOrRegisterActor0(name, actorFactory);
    }

    private <Message> ActorRef<Message> tryGetActor0(final String rootName) throws SuspendExecution, RuntimeException {
        final Store store = grid.store();
        final StoreTransaction txn = store.beginTransaction();
        try {
            try {
                final long root = store.getRoot(rootName, null);
                byte[] buf = store.get(root);
                if (buf == null) {
                    LOG.debug("Store returned null for root {}", rootName);
                    return null;
                }
                store.commit(txn);

                LOG.debug("Store returned a buffer ({} bytes) for root {}", buf.length, rootName);

                if (buf.length == 0)
                    return null; // TODO: Galaxy should return null

                return (ActorRef<Message>) updateCache(rootName, root, deserActor(rootName, buf));
            } catch (TimeoutException e) {
                LOG.error("Getting actor {} failed due to timeout", rootName);
                store.rollback(txn);
                store.abort(txn);
                throw new RuntimeException("Actor discovery failed");
            }
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
View Full Code Here

        }
    }

    private <Message> ActorRef<Message> 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();

        final StoreTransaction txn = store.beginTransaction();
        try {
            root = store.getRoot(rootName, txn);

            store.setListener(root, new AbstractCacheListener() {
                @Override
                public void evicted(Cache cache, long id) {
                    invalidated(cache, id);
                }

                @Override
                public void invalidated(Cache cache, long id) {
                    grid1.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);
                    }
                }
            });

            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.get(root);

            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)
                            return null; // throw new java.util.concurrent.TimeoutException();
                        cond.await(deadline - now, TimeUnit.NANOSECONDS);
                    } else
                        cond.await();
                    buf = store.get(root);
                }
            } finally {
                lck0.unlock();
            }
View Full Code Here

            throw new RuntimeException("Actor discovery failed");
        }
    }

    private <Message> ActorRef<Message> getOrRegisterActor0(final String rootName, Callable<ActorRef<Message>> 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<Message> actor;
                byte[] buf = store.getx(root, txn);
                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);
                } else
                    actor = deserActor(rootName, buf);

                store.commit(txn);

                return (ActorRef<Message>) updateCache(rootName, root, actor);
            } 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);
        }
View Full Code Here

            return null;
        }
    }

    private ActorRef<?> updateCache(final String rootName, long root, ActorRef<?> actor) {
        final Store store = grid.store();
        store.setListener(root, new AbstractCacheListener() {
            @Override
            public void invalidated(Cache cache, long id) {
                evicted(cache, id);
            }

            @Override
            public void received(Cache cache, long id, long version, ByteBuffer data) {
                evicted(cache, id);
            }

            @Override
            public void evicted(Cache cache, long id) {
                rootCache.remove(rootName);
                store.setListener(id, null);
            }
        });
        rootCache.put(rootName, actor);
        return actor;
    }
View Full Code Here

    public Object register(LocalActor<?, ?> actor) throws SuspendExecution {
        final String rootName = actor.getName();

        LOG.info("Registering actor {} at root {}", actor, rootName);

        final Store store = grid.store();
        StoreTransaction txn = store.beginTransaction();
        serlock.lock();
        try {
            try {
                final long root = store.getRoot(rootName, txn);
                store.getx(root, txn);
                store.set(root, ser.write(actor), txn);
                LOG.debug("commit Registering actor {} at rootId  {}", actor, root);
                store.commit(txn);
                RemoteChannelReceiver.getReceiver(actor.getMailbox(), true).handleRefMessage(new GlxRemoteChannel.RefMessage(true, grid.cluster().getMyNodeId()));
                return root; // root is the global id
            } catch (TimeoutException e) {
                LOG.error("Registering actor {} at root {} failed due to timeout", actor, rootName);
                store.rollback(txn);
                store.abort(txn);
                throw new RuntimeException("Actor registration failed");
            }
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally {
View Full Code Here

TOP

Related Classes of co.paralleluniverse.galaxy.quasar.Store

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.