Search in sources :

Example 11 with KeyInfo

use of doitincloud.rdbcache.models.KeyInfo in project rdbcache by rdbcache.

the class RedisRepoImpl method find.

@Override
public boolean find(final Context context, final KvPairs pairs, final AnyKey anyKey) {
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("find pairs(" + pairs.size() + "): " + pairs.printKey() + "anyKey(" + anyKey.size() + "): " + anyKey.printTable());
    }
    boolean foundAll = true;
    for (int i = 0; i < pairs.size(); i++) {
        KvPair pair = pairs.get(i);
        String key = pair.getId();
        String type = pair.getType();
        KeyInfo keyInfo = anyKey.getAny(i);
        String hashKey = hdataPrefix + "::" + type + ":" + key;
        Map<String, Object> map = null;
        if (enableDataCache) {
            map = (Map<String, Object>) AppCtx.getCacheOps().getData(pair.getIdType());
            if (map != null && map.size() > 0) {
                pair.setData(map);
                LOGGER.debug("find - found from cache " + key);
            }
        }
        if (map == null) {
            StopWatch stopWatch = context.startStopWatch("redis", "hashOps.entries");
            try {
                map = hashOps.entries(hashKey);
                if (stopWatch != null)
                    stopWatch.stopNow();
                if (map != null && map.size() > 0) {
                    pair.setData(map);
                    if (enableDataCache) {
                        AppCtx.getCacheOps().putData(pair, keyInfo);
                    }
                    LOGGER.debug("find - found from redis " + key);
                }
            } catch (Exception e) {
                if (stopWatch != null)
                    stopWatch.stopNow();
                foundAll = false;
                String msg = e.getCause().getMessage();
                LOGGER.error(msg);
                context.logTraceMessage(msg);
                e.printStackTrace();
                if (context.isSync()) {
                    throw new ServerErrorException(context, msg);
                }
                continue;
            }
        }
        if (map == null || map.size() == 0) {
            foundAll = false;
            LOGGER.debug("find - not found " + key);
            continue;
        }
    }
    LOGGER.trace("find returns " + foundAll);
    return foundAll;
}
Also used : KvPair(doitincloud.rdbcache.models.KvPair) KeyInfo(doitincloud.rdbcache.models.KeyInfo) ServerErrorException(doitincloud.commons.exceptions.ServerErrorException) ServerErrorException(doitincloud.commons.exceptions.ServerErrorException) StopWatch(doitincloud.rdbcache.models.StopWatch)

Example 12 with KeyInfo

use of doitincloud.rdbcache.models.KeyInfo in project rdbcache by rdbcache.

the class RedisRepoImpl method save.

@Override
public boolean save(final Context context, final KvPairs pairs, final AnyKey anyKey) {
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("save pairs(" + pairs.size() + "): " + pairs.printKey() + "anyKey(" + anyKey.size() + "): " + anyKey.printTable());
    }
    boolean savedAll = true;
    for (int i = 0; i < pairs.size(); i++) {
        KvPair pair = pairs.get(i);
        String key = pair.getId();
        String type = pair.getType();
        String hashKey = hdataPrefix + "::" + type + ":" + key;
        KeyInfo keyInfo = anyKey.getAny(i);
        Map<String, Object> map = pair.getData();
        if (enableDataCache) {
            AppCtx.getCacheOps().putData(pair, keyInfo);
        }
        StopWatch stopWatch = context.startStopWatch("redis", "hashOps.putAll");
        try {
            hashOps.putAll(hashKey, map);
            if (stopWatch != null)
                stopWatch.stopNow();
            LOGGER.debug("save to redis for " + key);
        } catch (Exception e) {
            if (stopWatch != null)
                stopWatch.stopNow();
            if (enableDataCache) {
                AppCtx.getCacheOps().removeData(pair.getIdType());
            }
            savedAll = false;
            String msg = e.getCause().getMessage();
            LOGGER.error(msg);
            context.logTraceMessage(msg);
            e.printStackTrace();
            if (context.isSync()) {
                throw new ServerErrorException(context, msg);
            }
        }
    }
    if (LOGGER.isTraceEnabled())
        LOGGER.trace("save returns " + savedAll);
    return savedAll;
}
Also used : KvPair(doitincloud.rdbcache.models.KvPair) KeyInfo(doitincloud.rdbcache.models.KeyInfo) ServerErrorException(doitincloud.commons.exceptions.ServerErrorException) ServerErrorException(doitincloud.commons.exceptions.ServerErrorException) StopWatch(doitincloud.rdbcache.models.StopWatch)

Example 13 with KeyInfo

use of doitincloud.rdbcache.models.KeyInfo 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 14 with KeyInfo

use of doitincloud.rdbcache.models.KeyInfo in project rdbcache by rdbcache.

the class RdbcacheApis method put_post.

/**
 * put_post post/put single item
 *
 * To update a key with partial data based on the key and/or query string.
 * It returns immediately, and asynchronously updates to redis and database
 *
 * @param request HttpServletRequest
 * @param key String, hash key
 * @param opt1 String, can be expire or table or "sync" or "async"
 * @param opt2 String, can be expire or table or "sync" or "async", but not otp1
 * @param opt3 String, can be expire or table or "sync" or "async", but not otp1 and opt2
 * @return ResponseEntity
 */
@RequestMapping(value = { "/rdbcache/v1/put/{key}", "/rdbcache/v1/put/{key}/{opt1}", "/rdbcache/v1/put/{key}/{opt1}/{opt2}", "/rdbcache/v1/put/{key}/{opt1}/{opt2}/{opt3}" }, method = { RequestMethod.POST, RequestMethod.PUT })
public ResponseEntity<?> put_post(HttpServletRequest request, @PathVariable("key") String key, @PathVariable Optional<String> opt1, @PathVariable Optional<String> opt2, @PathVariable Optional<String> opt3, @RequestBody String value) {
    if (value == null || value.length() == 0) {
        throw new BadRequestException("missing request body");
    }
    Context context = new Context();
    KvPairs pairs = new KvPairs(key, value);
    AnyKey anyKey = Request.process(context, request, pairs, opt1, opt2, opt3);
    LOGGER.trace(anyKey.print() + " pairs(" + pairs.size() + "): " + pairs.printKey());
    KeyInfo keyInfo = anyKey.getKeyInfo();
    if (key.equals("*") && keyInfo.getQuery() == null) {
        AppCtx.getAsyncOps().doSaveToRedisAndDbase(context, pairs, anyKey);
    } else {
        AppCtx.getAsyncOps().doPutOperation(context, pairs, anyKey);
    }
    return Response.send(context, pairs);
}
Also used : Context(doitincloud.rdbcache.supports.Context) AnyKey(doitincloud.rdbcache.supports.AnyKey) KeyInfo(doitincloud.rdbcache.models.KeyInfo) BadRequestException(doitincloud.commons.exceptions.BadRequestException) KvPairs(doitincloud.rdbcache.supports.KvPairs)

Example 15 with KeyInfo

use of doitincloud.rdbcache.models.KeyInfo in project rdbcache by rdbcache.

the class RdbcacheApis method push_post.

/**
 * push_post post multiple items
 *
 * To update one or more entries based on input key and value map. No * key. No query string.
 * It returns immediately, and asynchronously updates redis and database
 *
 * @param request HttpServletRequest
 * @param opt1 String, can be expire or table or "sync" or "async"
 * @param opt2 String, can be expire or table or "sync" or "async", but not otp1
 * @param opt3 String, can be expire or table or "sync" or "async", but not otp1 and opt2
 * @param map Map, a map of key and value pairs
 * @return ResponseEntity
 */
@RequestMapping(value = { "/rdbcache/v1/push", "/rdbcache/v1/push/{opt1}", "/rdbcache/v1/push/{opt1}/{opt2}", "/rdbcache/v1/push/{opt1}/{opt2}/{opt3}" }, method = RequestMethod.POST)
public ResponseEntity<?> push_post(HttpServletRequest request, @PathVariable Optional<String> opt1, @PathVariable Optional<String> opt2, @PathVariable Optional<String> opt3, @RequestBody Map<String, Object> map) {
    if (request.getParameterMap().size() != 0) {
        throw new BadRequestException("query string is not supported");
    }
    if (map == null || map.size() == 0) {
        throw new BadRequestException("missing request body");
    }
    if (map.containsKey("*")) {
        throw new BadRequestException("no * allowed as key");
    }
    if (request.getParameterMap().size() > 0) {
        throw new BadRequestException("query string is not supported");
    }
    Context context = new Context(false, true);
    KvPairs pairs = new KvPairs(map);
    AnyKey anyKey = Request.process(context, request, pairs, opt1, opt2, opt3);
    if (anyKey.size() != map.size()) {
        throw new BadRequestException("one or more keys not found");
    }
    for (int i = 0; i < anyKey.size(); i++) {
        KvPair pair = pairs.get(i);
        KeyInfo keyInfo = anyKey.get(i);
        if (keyInfo.getIsNew()) {
            throw new BadRequestException("key not found for " + pair.getId());
        }
    }
    LOGGER.trace(anyKey.print() + " pairs(" + pairs.size() + "): " + pairs.printKey());
    AppCtx.getAsyncOps().doPushOperations(context, pairs, anyKey);
    return Response.send(context, pairs);
}
Also used : Context(doitincloud.rdbcache.supports.Context) AnyKey(doitincloud.rdbcache.supports.AnyKey) KvPair(doitincloud.rdbcache.models.KvPair) KeyInfo(doitincloud.rdbcache.models.KeyInfo) BadRequestException(doitincloud.commons.exceptions.BadRequestException) KvPairs(doitincloud.rdbcache.supports.KvPairs)

Aggregations

KeyInfo (doitincloud.rdbcache.models.KeyInfo)50 KvPair (doitincloud.rdbcache.models.KvPair)29 AnyKey (doitincloud.rdbcache.supports.AnyKey)21 Context (doitincloud.rdbcache.supports.Context)17 KvPairs (doitincloud.rdbcache.supports.KvPairs)17 Test (org.junit.Test)13 ServerErrorException (doitincloud.commons.exceptions.ServerErrorException)9 StopWatch (doitincloud.rdbcache.models.StopWatch)8 BadRequestException (doitincloud.commons.exceptions.BadRequestException)7 SQLException (java.sql.SQLException)4 QueryInfo (doitincloud.rdbcache.queries.QueryInfo)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 MockServletContext (org.springframework.mock.web.MockServletContext)2 NotFoundException (doitincloud.commons.exceptions.NotFoundException)1 CacheOps (doitincloud.rdbcache.services.CacheOps)1 DbaseOps (doitincloud.rdbcache.services.DbaseOps)1 ExpireDbOps (doitincloud.rdbcache.supports.ExpireDbOps)1