Search in sources :

Example 6 with AnyKey

use of doitincloud.rdbcache.supports.AnyKey in project rdbcache by rdbcache.

the class ExpireOps method onExpireEvent.

/**
 * To process key expired event
 *
 * @param event key expired event
 */
public void onExpireEvent(String event) {
    LOGGER.debug("Received: " + event);
    if (!event.startsWith(eventPrefix)) {
        return;
    }
    String[] parts = event.split("::");
    if (parts.length < 3) {
        LOGGER.error("invalid event format");
        return;
    }
    String hashKey = parts[1];
    int index = hashKey.indexOf(":");
    if (index < 0) {
        LOGGER.error("invalid event format, failed to figure out type and key");
        return;
    }
    String type = hashKey.substring(0, index);
    String key = hashKey.substring(index + 1);
    String traceId = parts[2];
    Context context = new Context(traceId);
    KvPair pair = new KvPair(key, type);
    if (enableMonitor)
        context.enableMonitor(event, "event", key);
    String lockKey = "lock_" + eventPrefix + "::" + hashKey + "::" + traceId;
    String signature = Utils.generateId();
    StopWatch stopWatch = context.startStopWatch("redis", "scriptExecutor.execute");
    String result = scriptExecutor.execute(expire_event_lock_script, Collections.singletonList(lockKey), signature, eventLockTimeout.toString());
    if (stopWatch != null)
        stopWatch.stopNow();
    if (!result.equals("OK")) {
        String msg = "unable to lock key: " + lockKey;
        LOGGER.trace(msg);
        context.closeMonitor();
        return;
    }
    try {
        KvPairs pairs = new KvPairs(pair);
        AnyKey anyKey = new AnyKey();
        if (!AppCtx.getKeyInfoRepo().find(context, pairs, anyKey)) {
            String msg = "keyInfo not found";
            LOGGER.error(msg);
            context.logTraceMessage(msg);
            return;
        }
        KeyInfo keyInfo = anyKey.getKeyInfo();
        LOGGER.trace(keyInfo.toString());
        Long expire = Long.valueOf(keyInfo.getExpire());
        if (expire > 0) {
            if (AppCtx.getRedisRepo().find(context, pairs, anyKey)) {
                String qkey = keyInfo.getQueryKey();
                if (qkey == null || !qkey.equals("NOOPS")) {
                    AppCtx.getDbaseRepo().save(context, pairs, anyKey);
                } else if (qkey != null && qkey.startsWith("ExpireDbOps::")) {
                    String beanName = qkey.substring(13);
                    ApplicationContext ctx = AppCtx.getApplicationContext();
                    if (ctx != null) {
                        try {
                            ExpireDbOps ops = (ExpireDbOps) ctx.getBean(beanName);
                            if (ops != null) {
                                ops.save(context, pairs, anyKey);
                            } else {
                                LOGGER.error("failed to get bean: " + beanName);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                } else {
                    LOGGER.trace("queryKey = " + keyInfo.getQueryKey());
                }
                AppCtx.getRedisRepo().delete(context, pairs, anyKey);
                AppCtx.getKeyInfoRepo().delete(context, pairs);
            } else {
                String msg = "failed to find key from redis for " + key;
                LOGGER.error(msg);
                context.logTraceMessage(msg);
            }
        }
        if (expire < 0) {
            if (AppCtx.getDbaseRepo().find(context, pairs, anyKey)) {
                AppCtx.getRedisRepo().save(context, pairs, anyKey);
                setExpireKey(context, pairs, anyKey);
            } else {
                String msg = "failed to find key from database for " + key;
                LOGGER.error(msg);
                context.logTraceMessage(msg);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        String msg = e.getCause().getMessage();
        LOGGER.error(msg);
        context.logTraceMessage(msg);
    } finally {
        stopWatch = context.startStopWatch("redis", "scriptExecutor.execute");
        scriptExecutor.execute(expire_event_unlock_script, Collections.singletonList(lockKey), signature);
        if (stopWatch != null)
            stopWatch.stopNow();
        context.closeMonitor();
    }
}
Also used : Context(doitincloud.rdbcache.supports.Context) ApplicationContext(org.springframework.context.ApplicationContext) AnyKey(doitincloud.rdbcache.supports.AnyKey) KvPair(doitincloud.rdbcache.models.KvPair) KvPairs(doitincloud.rdbcache.supports.KvPairs) StopWatch(doitincloud.rdbcache.models.StopWatch) ApplicationContext(org.springframework.context.ApplicationContext) KeyInfo(doitincloud.rdbcache.models.KeyInfo) ExpireDbOps(doitincloud.rdbcache.supports.ExpireDbOps)

Example 7 with AnyKey

use of doitincloud.rdbcache.supports.AnyKey in project rdbcache by rdbcache.

the class DbaseRepoImpl method find.

@Override
public boolean find(final Context context, final KvPair pair, final KeyInfo keyInfo) {
    KvPairs pairs = new KvPairs(pair);
    AnyKey anyKey = new AnyKey(keyInfo);
    return find(context, pairs, anyKey);
}
Also used : AnyKey(doitincloud.rdbcache.supports.AnyKey) KvPairs(doitincloud.rdbcache.supports.KvPairs)

Example 8 with AnyKey

use of doitincloud.rdbcache.supports.AnyKey in project rdbcache by rdbcache.

the class DbaseRepoImpl method delete.

@Override
public boolean delete(final Context context, final KvPair pair, final KeyInfo keyInfo) {
    KvPairs pairs = new KvPairs(pair);
    AnyKey anyKey = new AnyKey(keyInfo);
    return delete(context, pairs, anyKey);
}
Also used : AnyKey(doitincloud.rdbcache.supports.AnyKey) KvPairs(doitincloud.rdbcache.supports.KvPairs)

Example 9 with AnyKey

use of doitincloud.rdbcache.supports.AnyKey in project rdbcache by rdbcache.

the class DbaseRepoImpl method find.

@Override
public boolean find(final Context context, final KvPairs pairs, final AnyKey anyKey) {
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("find: " + pairs.printKey() + anyKey.print());
    }
    String table = anyKey.getKeyInfo().getTable();
    if (table == null) {
        if (!kvFind(context, pairs, anyKey)) {
            LOGGER.debug("find - kvFind failed: " + pairs.printKey());
            return false;
        } else {
            LOGGER.debug("find - kvFind Ok: " + pairs.printKey());
        }
        AppCtx.getKeyInfoRepo().save(context, pairs, anyKey);
        return true;
    } else {
        JdbcTemplate jdbcTemplate = AppCtx.getDbaseOps().getJdbcTemplate(context, table);
        boolean allOk = true;
        if (anyKey.size() == 1) {
            Query query = new Query(context, jdbcTemplate, pairs, anyKey);
            if (!query.ifSelectOk() || !query.executeSelect()) {
                if (enableDbFallback) {
                    if (!kvFind(context, pairs, anyKey)) {
                        String msg = "find - not found fallbacked to default table: " + pairs.printKey();
                        LOGGER.trace(msg);
                        allOk = false;
                    } else {
                        String msg = "find - found fallbacked to default table Ok: " + pairs.printKey();
                        context.logTraceMessage(msg);
                        LOGGER.warn(msg);
                    }
                } else {
                    LOGGER.debug("find - not found: from " + table + " " + pairs.printKey());
                    allOk = false;
                }
            } else {
                LOGGER.debug("find - found Ok: from " + table + " " + pairs.printKey());
                AppCtx.getKeyInfoRepo().save(context, pairs, anyKey);
            }
        } else {
            for (int i = 0; i < anyKey.size(); i++) {
                KeyInfo keyInfo = anyKey.get(i);
                if (keyInfo.getQuery() != null) {
                    throw new ServerErrorException("query for muliple keyInfos is not supported");
                }
                table = keyInfo.getTable();
                AnyKey newAnyKey = new AnyKey(keyInfo);
                KvPair pair = pairs.getAny(i);
                KvPairs newPairs = new KvPairs(pair);
                Query query = new Query(context, jdbcTemplate, newPairs, newAnyKey);
                if (!query.ifSelectOk() || !query.executeSelect()) {
                    if (enableDbFallback) {
                        if (!kvFind(context, newPairs, newAnyKey)) {
                            String msg = "find - not found fallbacked to default table: " + newPairs.printKey();
                            LOGGER.trace(msg);
                            allOk = false;
                        } else {
                            String msg = "find - found fallbacked to default table Ok: " + newPairs.printKey();
                            context.logTraceMessage(msg);
                            LOGGER.warn(msg);
                        }
                    } else {
                        LOGGER.debug("find - not found: from " + table + " " + newPairs.printKey());
                        allOk = false;
                    }
                } else {
                    LOGGER.debug("find - found Ok: from " + table + " " + pairs.printKey());
                    AppCtx.getKeyInfoRepo().save(context, newPairs, newAnyKey);
                }
            }
        }
        return allOk;
    }
}
Also used : AnyKey(doitincloud.rdbcache.supports.AnyKey) Query(doitincloud.rdbcache.queries.Query) KvPairs(doitincloud.rdbcache.supports.KvPairs) ServerErrorException(doitincloud.commons.exceptions.ServerErrorException) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate)

Example 10 with AnyKey

use of doitincloud.rdbcache.supports.AnyKey in project rdbcache by rdbcache.

the class DbaseRepoImpl method insert.

@Override
public boolean insert(final Context context, final KvPair pair, final KeyInfo keyInfo) {
    KvPairs pairs = new KvPairs(pair);
    AnyKey anyKey = new AnyKey(keyInfo);
    return insert(context, pairs, anyKey);
}
Also used : AnyKey(doitincloud.rdbcache.supports.AnyKey) KvPairs(doitincloud.rdbcache.supports.KvPairs)

Aggregations

AnyKey (doitincloud.rdbcache.supports.AnyKey)36 KvPairs (doitincloud.rdbcache.supports.KvPairs)32 Context (doitincloud.rdbcache.supports.Context)23 KeyInfo (doitincloud.rdbcache.models.KeyInfo)21 KvPair (doitincloud.rdbcache.models.KvPair)14 BadRequestException (doitincloud.commons.exceptions.BadRequestException)12 Test (org.junit.Test)9 NotFoundException (doitincloud.commons.exceptions.NotFoundException)4 ServerErrorException (doitincloud.commons.exceptions.ServerErrorException)4 StopWatch (doitincloud.rdbcache.models.StopWatch)2 QueryInfo (doitincloud.rdbcache.queries.QueryInfo)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 MockServletContext (org.springframework.mock.web.MockServletContext)2 Query (doitincloud.rdbcache.queries.Query)1 CacheOps (doitincloud.rdbcache.services.CacheOps)1 ExpireDbOps (doitincloud.rdbcache.supports.ExpireDbOps)1 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 SQLException (java.sql.SQLException)1