Search in sources :

Example 1 with WithProfilingCallback

use of org.killbill.commons.profiling.Profiling.WithProfilingCallback in project killbill by killbill.

the class EntitySqlDaoWrapperInvocationHandler method invokeWithCaching.

private Object invokeWithCaching(final Cachable cachableAnnotation, final Method method, final Object[] args) throws Throwable {
    final ObjectType objectType = getObjectType();
    final CacheType cacheType = cachableAnnotation.value();
    final CacheController<Object, Object> cache = cacheControllerDispatcher.getCacheController(cacheType);
    Object result = null;
    if (cache != null) {
        // Find all arguments marked with @CachableKey
        final Map<Integer, Object> keyPieces = new LinkedHashMap<Integer, Object>();
        final Annotation[][] annotations = method.getParameterAnnotations();
        for (int i = 0; i < annotations.length; i++) {
            for (int j = 0; j < annotations[i].length; j++) {
                final Annotation annotation = annotations[i][j];
                if (CachableKey.class.equals(annotation.annotationType())) {
                    // CachableKey position starts at 1
                    keyPieces.put(((CachableKey) annotation).value() - 1, args[i]);
                    break;
                }
            }
        }
        // Build the Cache key
        final String cacheKey = buildCacheKey(keyPieces);
        final InternalTenantContext internalTenantContext = (InternalTenantContext) Iterables.find(ImmutableList.copyOf(args), new Predicate<Object>() {

            @Override
            public boolean apply(final Object input) {
                return input instanceof InternalTenantContext;
            }
        }, null);
        final CacheLoaderArgument cacheLoaderArgument = new CacheLoaderArgument(objectType, args, internalTenantContext, handle);
        return cache.get(cacheKey, cacheLoaderArgument);
    }
    if (result == null) {
        result = prof.executeWithProfiling(ProfilingFeatureType.DAO_DETAILS, sqlDaoClass.getSimpleName() + "(raw) :" + method.getName(), new WithProfilingCallback() {

            @Override
            public Object execute() throws Throwable {
                return method.invoke(sqlDao, args);
            }
        });
    }
    return result;
}
Also used : Annotation(java.lang.annotation.Annotation) CacheLoaderArgument(org.killbill.billing.util.cache.CacheLoaderArgument) CacheType(org.killbill.billing.util.cache.Cachable.CacheType) LinkedHashMap(java.util.LinkedHashMap) Predicate(com.google.common.base.Predicate) WithProfilingCallback(org.killbill.commons.profiling.Profiling.WithProfilingCallback) ObjectType(org.killbill.billing.ObjectType) InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext) CachableKey(org.killbill.billing.util.cache.CachableKey)

Example 2 with WithProfilingCallback

use of org.killbill.commons.profiling.Profiling.WithProfilingCallback in project killbill by killbill.

the class TestJanitor method testDBRouterThreadState.

@Test(groups = "slow")
public void testDBRouterThreadState() throws Throwable {
    final Payment payment = (Payment) DBRouterUntyped.withRODBIAllowed(true, new WithProfilingCallback<Object, Throwable>() {

        @Override
        public Payment execute() throws Throwable {
            // Shouldn't happen in practice, but it's just to verify the behavior
            assertEquals(DBRouterUntyped.getCurrentState(), THREAD_STATE.RO_ALLOWED);
            final BigDecimal requestedAmount = BigDecimal.TEN;
            testListener.pushExpectedEvent(NextEvent.PAYMENT);
            final Payment payment = paymentApi.createAuthorization(account, account.getPaymentMethodId(), null, requestedAmount, account.getCurrency(), null, UUID.randomUUID().toString(), UUID.randomUUID().toString(), ImmutableList.<PluginProperty>of(), callContext);
            testListener.assertListenerStatus();
            // Thread switch, RW by default
            assertEquals(mockPaymentProviderPlugin.getLastThreadState(), THREAD_STATE.RW_ONLY);
            // Switched to RW, because of RW DAO call
            assertEquals(DBRouterUntyped.getCurrentState(), THREAD_STATE.RW_ONLY);
            return payment;
        }
    });
    DBRouterUntyped.withRODBIAllowed(true, new WithProfilingCallback<Object, Throwable>() {

        @Override
        public Object execute() throws Throwable {
            assertEquals(DBRouterUntyped.getCurrentState(), THREAD_STATE.RO_ALLOWED);
            final Payment retrievedPayment2 = paymentApi.getPayment(payment.getId(), true, false, ImmutableList.<PluginProperty>of(), callContext);
            Assert.assertEquals(retrievedPayment2.getTransactions().get(0).getTransactionStatus(), TransactionStatus.SUCCESS);
            // No thread switch, RO as well
            assertEquals(mockPaymentProviderPlugin.getLastThreadState(), THREAD_STATE.RO_ALLOWED);
            assertEquals(DBRouterUntyped.getCurrentState(), THREAD_STATE.RO_ALLOWED);
            return null;
        }
    });
}
Also used : PluginProperty(org.killbill.billing.payment.api.PluginProperty) Payment(org.killbill.billing.payment.api.Payment) BigDecimal(java.math.BigDecimal) WithProfilingCallback(org.killbill.commons.profiling.Profiling.WithProfilingCallback) Test(org.testng.annotations.Test)

Aggregations

WithProfilingCallback (org.killbill.commons.profiling.Profiling.WithProfilingCallback)2 Predicate (com.google.common.base.Predicate)1 Annotation (java.lang.annotation.Annotation)1 BigDecimal (java.math.BigDecimal)1 LinkedHashMap (java.util.LinkedHashMap)1 ObjectType (org.killbill.billing.ObjectType)1 InternalTenantContext (org.killbill.billing.callcontext.InternalTenantContext)1 Payment (org.killbill.billing.payment.api.Payment)1 PluginProperty (org.killbill.billing.payment.api.PluginProperty)1 CacheType (org.killbill.billing.util.cache.Cachable.CacheType)1 CachableKey (org.killbill.billing.util.cache.CachableKey)1 CacheLoaderArgument (org.killbill.billing.util.cache.CacheLoaderArgument)1 Test (org.testng.annotations.Test)1