Search in sources :

Example 1 with RoutingPolicy

use of com.yahoo.messagebus.routing.RoutingPolicy in project vespa by vespa-engine.

the class ProtocolRepository method getRoutingPolicy.

/**
 * Creates and returns a routing policy that matches the given arguments. If a routing policy has been created
 * previously using the exact same parameters, this method will returned that cached instance instead of creating
 * another. Not that when you replace a protocol using {@link #putProtocol(Protocol)} the policy cache is cleared.
 *
 * @param protocolName The name of the protocol whose routing policy to create.
 * @param policyName   The name of the routing policy to create.
 * @param policyParam  The parameter to pass to the routing policy constructor.
 * @return The created routing policy.
 */
public RoutingPolicy getRoutingPolicy(String protocolName, String policyName, String policyParam) {
    String cacheKey = protocolName + "." + policyName + "." + policyParam;
    RoutingPolicy ret = routingPolicyCache.get(cacheKey);
    if (ret != null) {
        return ret;
    }
    synchronized (this) {
        Protocol protocol = getProtocol(protocolName);
        if (protocol == null) {
            log.log(LogLevel.ERROR, "Protocol '" + protocolName + "' not supported.");
            return null;
        }
        try {
            ret = protocol.createPolicy(policyName, policyParam);
        } catch (RuntimeException e) {
            log.log(LogLevel.ERROR, "Protcol '" + protocolName + "' threw an exception: " + e.getMessage(), e);
            return null;
        }
        if (ret == null) {
            log.log(LogLevel.ERROR, "Protocol '" + protocolName + "' failed to create routing policy '" + policyName + "' with parameter '" + policyParam + "'.");
            return null;
        }
        routingPolicyCache.put(cacheKey, ret);
    }
    return ret;
}
Also used : RoutingPolicy(com.yahoo.messagebus.routing.RoutingPolicy) Utf8String(com.yahoo.text.Utf8String)

Example 2 with RoutingPolicy

use of com.yahoo.messagebus.routing.RoutingPolicy in project vespa by vespa-engine.

the class MessageBusTestCase method testRoutingPolicyCache.

@Test
public void testRoutingPolicyCache() throws ListenFailedException, UnknownHostException {
    Slobrok slobrok = new Slobrok();
    String config = "slobrok[1]\nslobrok[0].connectionspec \"" + new Spec("localhost", slobrok.port()).toString() + "\"\n";
    SimpleProtocol protocol = new SimpleProtocol();
    protocol.addPolicyFactory("Custom", new CustomPolicyFactory());
    MessageBus bus = new MessageBus(new RPCNetwork(new RPCNetworkParams().setSlobrokConfigId("raw:" + config)), new MessageBusParams().addProtocol(protocol));
    RoutingPolicy all = bus.getRoutingPolicy(SimpleProtocol.NAME, "Custom", null);
    assertNotNull(all);
    RoutingPolicy ref = bus.getRoutingPolicy(SimpleProtocol.NAME, "Custom", null);
    assertNotNull(ref);
    assertSame(all, ref);
    RoutingPolicy allArg = bus.getRoutingPolicy(SimpleProtocol.NAME, "Custom", "Arg");
    assertNotNull(allArg);
    assertNotSame(all, allArg);
    RoutingPolicy refArg = bus.getRoutingPolicy(SimpleProtocol.NAME, "Custom", "Arg");
    assertNotNull(refArg);
    assertSame(allArg, refArg);
}
Also used : SimpleProtocol(com.yahoo.messagebus.test.SimpleProtocol) RPCNetworkParams(com.yahoo.messagebus.network.rpc.RPCNetworkParams) RPCNetwork(com.yahoo.messagebus.network.rpc.RPCNetwork) Slobrok(com.yahoo.jrt.slobrok.server.Slobrok) RoutingPolicy(com.yahoo.messagebus.routing.RoutingPolicy) Spec(com.yahoo.jrt.Spec) CustomPolicyFactory(com.yahoo.messagebus.routing.test.CustomPolicyFactory) Test(org.junit.Test)

Example 3 with RoutingPolicy

use of com.yahoo.messagebus.routing.RoutingPolicy in project vespa by vespa-engine.

the class ProtocolRepositoryTestCase method requireThatPolicyParamIsPartOfCacheKey.

@Test
public void requireThatPolicyParamIsPartOfCacheKey() {
    ProtocolRepository repo = new ProtocolRepository();
    SimpleProtocol protocol = new SimpleProtocol();
    protocol.addPolicyFactory("Custom", new MyFactory());
    repo.putProtocol(protocol);
    RoutingPolicy prev = repo.getRoutingPolicy(SimpleProtocol.NAME, "Custom", "foo");
    assertNotNull(prev);
    RoutingPolicy next = repo.getRoutingPolicy(SimpleProtocol.NAME, "Custom", "bar");
    assertNotNull(next);
    assertNotSame(prev, next);
}
Also used : SimpleProtocol(com.yahoo.messagebus.test.SimpleProtocol) RoutingPolicy(com.yahoo.messagebus.routing.RoutingPolicy) Test(org.junit.Test)

Example 4 with RoutingPolicy

use of com.yahoo.messagebus.routing.RoutingPolicy in project vespa by vespa-engine.

the class ProtocolRepositoryTestCase method requireThatCreatePolicyExceptionIsCaught.

@Test
public void requireThatCreatePolicyExceptionIsCaught() {
    ProtocolRepository repo = new ProtocolRepository();
    SimpleProtocol protocol = new SimpleProtocol();
    protocol.addPolicyFactory("Custom", new SimpleProtocol.PolicyFactory() {

        @Override
        public RoutingPolicy create(String param) {
            throw new RuntimeException();
        }
    });
    repo.putProtocol(protocol);
    assertNull(repo.getRoutingPolicy(SimpleProtocol.NAME, "Custom", null));
}
Also used : SimpleProtocol(com.yahoo.messagebus.test.SimpleProtocol) RoutingPolicy(com.yahoo.messagebus.routing.RoutingPolicy) Test(org.junit.Test)

Example 5 with RoutingPolicy

use of com.yahoo.messagebus.routing.RoutingPolicy in project vespa by vespa-engine.

the class ProtocolRepositoryTestCase method requireThatPolicyIsCached.

@Test
public void requireThatPolicyIsCached() {
    ProtocolRepository repo = new ProtocolRepository();
    SimpleProtocol protocol = new SimpleProtocol();
    protocol.addPolicyFactory("Custom", new MyFactory());
    repo.putProtocol(protocol);
    RoutingPolicy prev = repo.getRoutingPolicy(SimpleProtocol.NAME, "Custom", null);
    assertNotNull(prev);
    RoutingPolicy next = repo.getRoutingPolicy(SimpleProtocol.NAME, "Custom", null);
    assertNotNull(next);
    assertSame(prev, next);
}
Also used : SimpleProtocol(com.yahoo.messagebus.test.SimpleProtocol) RoutingPolicy(com.yahoo.messagebus.routing.RoutingPolicy) Test(org.junit.Test)

Aggregations

RoutingPolicy (com.yahoo.messagebus.routing.RoutingPolicy)5 SimpleProtocol (com.yahoo.messagebus.test.SimpleProtocol)4 Test (org.junit.Test)4 Spec (com.yahoo.jrt.Spec)1 Slobrok (com.yahoo.jrt.slobrok.server.Slobrok)1 RPCNetwork (com.yahoo.messagebus.network.rpc.RPCNetwork)1 RPCNetworkParams (com.yahoo.messagebus.network.rpc.RPCNetworkParams)1 CustomPolicyFactory (com.yahoo.messagebus.routing.test.CustomPolicyFactory)1 Utf8String (com.yahoo.text.Utf8String)1