Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added AnnotationHookProvider
  • Loading branch information
quilombodigital committed Apr 23, 2015
1 parent f7302ed commit 437781b
Show file tree
Hide file tree
Showing 21 changed files with 425 additions and 71 deletions.
Expand Up @@ -30,6 +30,7 @@

import com.ea.orbit.actors.IAddressable;
import com.ea.orbit.actors.IRemindable;
import com.ea.orbit.actors.cluster.INodeAddress;
import com.ea.orbit.concurrent.Task;

import java.lang.reflect.Method;
Expand Down Expand Up @@ -114,11 +115,24 @@ public interface IRuntime
String runtimeIdentity();

/**
* Gets current Activation, the actor reference that is currently invoking the method
*
* @return
* @return caller actor
*/
ActorReference getCurrentActivation();

/**
* Gets a long representing the current tracing id
*
* @return unique trace id
*/
long getCurrentTraceId();

/**
* Gets an address for an active actor
*
* @return actor address, null if actor is not active
*/
Task<INodeAddress> locateActiveActor(final IAddressable actorReference);

}
Expand Up @@ -1027,7 +1027,7 @@ public Task<?> sendMessage(IAddressable toReference, boolean oneWay, final int m
if (toNode == null)
{
// TODO: Ensure that both paths encode exception the same way.
return hosting.locateActor(actorReference)
return hosting.locateAndActivateActor(actorReference)
.thenCompose(x -> messaging.sendMessage(x, oneWay, actorReference._interfaceId(), methodId, actorReference.id, params));
}
return messaging.sendMessage(toNode, oneWay, actorReference._interfaceId(), methodId, actorReference.id, params);
Expand Down Expand Up @@ -1141,4 +1141,8 @@ public void activationCleanup(final boolean block)
Task.allOf(futures).join();
}
}

public Task<INodeAddress> locateActiveActor(final IAddressable actorReference){
return hosting.locateActiveActor(actorReference);
}
}
Expand Up @@ -167,7 +167,18 @@ private void updateServerNodes()
}
}

public Task<INodeAddress> locateActor(final IAddressable actorReference)
public Task<INodeAddress> locateActiveActor(final IAddressable actorReference){
ActorKey addressable = new ActorKey(((ActorReference) actorReference)._interfaceClass().getName(),
String.valueOf(((ActorReference) actorReference).id));
INodeAddress address = localAddressCache.get(addressable);
if (address != null && activeNodes.containsKey(address))
{
return Task.fromValue(address);
}
return Task.fromValue(null);
}

public Task<INodeAddress> locateAndActivateActor(final IAddressable actorReference)
{
ActorKey addressable = new ActorKey(((ActorReference) actorReference)._interfaceClass().getName(),
String.valueOf(((ActorReference) actorReference).id));
Expand Down
Expand Up @@ -138,7 +138,7 @@ public void ensureNoObjectsAreCreatedClientTest() throws ExecutionException, Int
ISomeActor player = IActor.getReference(ISomeActor.class, String.valueOf(i));
client.bind();
assertEquals("bla", player.sayHello("meh").join());
assertTrue(serverAddresses.contains(client.getHosting().locateActor((ActorReference<?>) player).join()));
assertTrue(serverAddresses.contains(client.getHosting().locateAndActivateActor((ActorReference<?>) player).join()));
}

}
Expand Down
10 changes: 10 additions & 0 deletions samples/annotation/README.md
@@ -0,0 +1,10 @@

Annotation Sample
===================

This is a custom annotation provider.





6 changes: 3 additions & 3 deletions samples/memoize/pom.xml → samples/annotation/pom.xml
Expand Up @@ -37,8 +37,8 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
</parent>
<modelVersion>4.0.0</modelVersion>

<name>Orbit Samples: Memoize</name>
<artifactId>orbit-samples-memoize</artifactId>
<name>Orbit Samples: Annotation</name>
<artifactId>orbit-samples-annotation</artifactId>

<dependencies>
<dependency>
Expand Down Expand Up @@ -96,7 +96,7 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
<artifactId>exec-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<mainClass>com.ea.orbit.samples.memoize.Main</mainClass>
<mainClass>com.ea.orbit.samples.customannotation.Main</mainClass>
</configuration>
</plugin>
</plugins>
Expand Down
Expand Up @@ -26,42 +26,41 @@
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package com.ea.orbit.samples.memoize;
package com.ea.orbit.samples.annotation;

import com.ea.orbit.actors.IAddressable;
import com.ea.orbit.actors.providers.IInvokeHookProvider;
import com.ea.orbit.actors.runtime.IRuntime;
import com.ea.orbit.concurrent.Task;

import net.jodah.expiringmap.ExpiringMap;

import java.lang.reflect.Method;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.ArrayList;
import java.util.List;

public class MemoizeHookProvider implements IInvokeHookProvider
public class AnnotationHookProvider implements IInvokeHookProvider
{

ExpiringMap<String, Task> memoizeMap = ExpiringMap.builder().variableExpiration().build();
private List<IAnnotationHandler> handlers = new ArrayList<>();

@Override
public Task invoke(IRuntime runtime, IAddressable toReference, Method m, boolean oneWay, int methodId, Object[] params)
{
Memoize memoizeAnnotation = m.getAnnotation(Memoize.class);
if (memoizeAnnotation != null)
for (IAnnotationHandler handler : handlers)
{
long memoizeMaxMillis = memoizeAnnotation.unit().toMillis(memoizeAnnotation.time());
String key = Integer.toString(methodId) + "_" + Stream.of(params).map(p -> Integer.toString(p.hashCode())).collect(Collectors.joining("_"));
Task cached = memoizeMap.get(key);
if (cached == null)
if (m.isAnnotationPresent(handler.annotationClass()))
{
cached = runtime.sendMessage(toReference, oneWay, methodId, params);
memoizeMap.put(key, cached, ExpiringMap.ExpirationPolicy.CREATED, memoizeMaxMillis, TimeUnit.MILLISECONDS);
Task<?> result = handler.invoke(m.getAnnotation(handler.annotationClass()), runtime, toReference, m, oneWay, methodId, params);
if (result != null)
{
return result;
}
}
return cached;
}
return runtime.sendMessage(toReference, oneWay, methodId, params);
}

public void addHandler(IAnnotationHandler handler)
{
handlers.add(handler);
}
}
@@ -0,0 +1,44 @@
/*
Copyright (C) 2015 Electronic Arts Inc. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of Electronic Arts, Inc. ("EA") nor the names of
its contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY ELECTRONIC ARTS AND ITS CONTRIBUTORS "AS IS" AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL ELECTRONIC ARTS OR ITS CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package com.ea.orbit.samples.annotation;

import com.ea.orbit.actors.IAddressable;
import com.ea.orbit.actors.runtime.IRuntime;
import com.ea.orbit.concurrent.Task;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

public interface IAnnotationHandler<T extends Annotation>
{
Class<T> annotationClass();

Task<?> invoke(T ann, IRuntime runtime, IAddressable toReference, Method m, boolean oneWay, int methodId, Object[] params);

}
Expand Up @@ -26,14 +26,15 @@
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package com.ea.orbit.samples.memoize;
package com.ea.orbit.samples.annotation.examples;

import com.ea.orbit.actors.IActor;
import com.ea.orbit.concurrent.Task;
import com.ea.orbit.samples.annotation.memoize.Memoize;

import java.util.concurrent.TimeUnit;

public interface IExample extends IActor
public interface IMemoizeExample extends IActor
{

@Memoize(time = 5, unit = TimeUnit.SECONDS)
Expand Down
@@ -0,0 +1,44 @@
/*
Copyright (C) 2015 Electronic Arts Inc. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of Electronic Arts, Inc. ("EA") nor the names of
its contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY ELECTRONIC ARTS AND ITS CONTRIBUTORS "AS IS" AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL ELECTRONIC ARTS OR ITS CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package com.ea.orbit.samples.annotation.examples;

import com.ea.orbit.actors.IActor;
import com.ea.orbit.concurrent.Task;
import com.ea.orbit.samples.annotation.onlyifactivated.OnlyIfActivated;

public interface IOnlyExample extends IActor
{

@OnlyIfActivated
Task<Void> doSomethingSpecial(String greeting);

Task<Void> makeActiveNow();

}

Expand Up @@ -26,14 +26,14 @@
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package com.ea.orbit.samples.memoize;
package com.ea.orbit.samples.annotation.examples;


import com.ea.orbit.actors.runtime.OrbitActor;
import com.ea.orbit.concurrent.Task;

@SuppressWarnings("rawtypes")
public class ExampleActor extends OrbitActor implements IExample
public class MemoizeExampleActor extends OrbitActor implements IMemoizeExample
{
public static int accessCount = 0;

Expand Down
@@ -0,0 +1,54 @@
/*
Copyright (C) 2015 Electronic Arts Inc. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the name of Electronic Arts, Inc. ("EA") nor the names of
its contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY ELECTRONIC ARTS AND ITS CONTRIBUTORS "AS IS" AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL ELECTRONIC ARTS OR ITS CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package com.ea.orbit.samples.annotation.examples;


import com.ea.orbit.actors.runtime.OrbitActor;
import com.ea.orbit.concurrent.Task;

@SuppressWarnings("rawtypes")
public class OnlyExampleActor extends OrbitActor implements IOnlyExample
{
public static int accessCount = 0;

@Override
public Task<Void> doSomethingSpecial(final String greeting)
{
accessCount++;
return Task.done();
}

@Override
public Task<Void> makeActiveNow()
{
//does really nothing, but allows the actor to be activated
return Task.done();
}
}

@@ -1,4 +1,4 @@
package com.ea.orbit.samples.memoize;
package com.ea.orbit.samples.annotation.memoize;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
Expand Down

0 comments on commit 437781b

Please sign in to comment.