Search in sources :

Example 1 with ExpireDbOps

use of doitincloud.rdbcache.supports.ExpireDbOps 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)

Aggregations

KeyInfo (doitincloud.rdbcache.models.KeyInfo)1 KvPair (doitincloud.rdbcache.models.KvPair)1 StopWatch (doitincloud.rdbcache.models.StopWatch)1 AnyKey (doitincloud.rdbcache.supports.AnyKey)1 Context (doitincloud.rdbcache.supports.Context)1 ExpireDbOps (doitincloud.rdbcache.supports.ExpireDbOps)1 KvPairs (doitincloud.rdbcache.supports.KvPairs)1 ApplicationContext (org.springframework.context.ApplicationContext)1