use of doitincloud.rdbcache.supports.KvPairs 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();
}
}
use of doitincloud.rdbcache.supports.KvPairs 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);
}
use of doitincloud.rdbcache.supports.KvPairs 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);
}
use of doitincloud.rdbcache.supports.KvPairs 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;
}
}
use of doitincloud.rdbcache.supports.KvPairs 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);
}
Aggregations