use of doitincloud.rdbcache.models.StopWatch 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.models.StopWatch in project rdbcache by rdbcache.
the class Query method executeInsert.
public boolean executeInsert(boolean enableLocal, boolean enableRedis) {
params = new ArrayList<>();
boolean allOk = true;
for (int i = 0; i < pairs.size(); i++) {
KvPair pair = pairs.get(i);
KeyInfo keyInfo = anyKey.getAny(i);
String table = keyInfo.getTable();
Map<String, Object> map = pair.getData();
String autoIncKey = AppCtx.getDbaseOps().getTableAutoIncColumn(context, table);
boolean cacheUpdate = false;
if (!map.containsKey(autoIncKey) && keyInfo.getParams() != null && keyInfo.getParams().size() == 1) {
String stdClause = "(" + autoIncKey + " = ?)";
if (stdClause.equals(keyInfo.getClause())) {
map.put(autoIncKey, keyInfo.getParams().get(0));
cacheUpdate = true;
}
}
Map<String, Object> columns = keyInfo.getColumns();
AppCtx.getDbaseOps().convertDbMap(columns, map);
String fields = "", values = "";
params.clear();
for (Map.Entry<String, Object> entry : map.entrySet()) {
params.add(entry.getValue());
if (fields.length() != 0) {
fields += ", ";
values += ", ";
}
fields += entry.getKey();
values += "?";
}
sql = "insert into " + table + " (" + fields + ") values (" + values + ")";
LOGGER.trace("sql: " + sql);
LOGGER.trace("params: " + params.toString());
int rowCount = 0;
KeyHolder keyHolder = new GeneratedKeyHolder();
StopWatch stopWatch = context.startStopWatch("dbase", "jdbcTemplate.update");
try {
rowCount = jdbcTemplate.update(new PreparedStatementCreator() {
@Override
public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
PreparedStatement ps;
ps = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
int i = 1;
for (Object param : params) {
ps.setObject(i++, param);
}
return ps;
}
}, keyHolder);
if (stopWatch != null)
stopWatch.stopNow();
} catch (Exception e) {
if (stopWatch != null)
stopWatch.stopNow();
allOk = false;
String msg = e.getCause().getMessage();
LOGGER.error(msg);
context.logTraceMessage(msg);
if (context.isSync()) {
throw new ServerErrorException(context, msg);
}
}
if (rowCount > 0) {
if (autoIncKey != null && keyHolder.getKey() != null) {
String keyValue = String.valueOf(keyHolder.getKey());
map.put(autoIncKey, keyValue);
cacheUpdate = true;
}
if (cacheUpdate) {
if (enableLocal) {
AppCtx.getCacheOps().putData(pair, keyInfo);
}
if (enableRedis) {
AppCtx.getRedisRepo().save(context, new KvPairs(pair), new AnyKey(keyInfo));
}
}
if (!Parser.prepareStandardClauseParams(context, pair, keyInfo)) {
String msg = "executeInsert failed when prepareStandardClauseParams for " + pair.getId();
LOGGER.error(msg);
context.logTraceMessage(msg);
if (context.isSync()) {
throw new ServerErrorException(context, msg);
}
}
LOGGER.trace("inserted " + pair.getId() + " into " + table);
} else {
allOk = false;
LOGGER.warn("failed to insert " + pair.getId() + " into " + table);
}
}
return allOk;
}
use of doitincloud.rdbcache.models.StopWatch in project rdbcache by rdbcache.
the class MonitorRepoImpl method save.
@Override
public void save(Monitor monitor) {
Map<String, Object> map = Utils.toMap(monitor);
String fields = "";
String values = "";
final List<Object> params1 = new ArrayList<>();
for (Map.Entry<String, Object> entry : map.entrySet()) {
params1.add(entry.getValue());
if (fields.length() != 0) {
fields += ", ";
values += ", ";
}
fields += entry.getKey();
values += "?";
}
final String sql1 = "insert into " + monitorTable + " (" + fields + ") values (" + values + ")";
int result = 0;
KeyHolder keyHolder = new GeneratedKeyHolder();
try {
result = jdbcTemplate.update(new PreparedStatementCreator() {
@Override
public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
PreparedStatement ps;
ps = connection.prepareStatement(sql1, Statement.RETURN_GENERATED_KEYS);
int i = 1;
for (Object param : params1) {
ps.setObject(i++, param);
}
return ps;
}
}, keyHolder);
} catch (Exception e) {
e.getStackTrace();
return;
}
if (result == 0) {
return;
}
Long id = Long.valueOf(keyHolder.getKey().toString());
List<StopWatch> watches = monitor.getStopWatches();
if (watches != null && watches.size() > 0) {
map = AppCtx.getDbaseOps().getTableColumns(null, stopWatchTable);
Set<String> keySet = map.keySet();
fields = "";
values = "";
for (String key : keySet) {
if (fields.length() != 0) {
fields += ", ";
values += ", ";
}
fields += key;
values += "?";
}
final String sql2 = "insert into " + monitorTable + "(" + fields + ") values (" + values + ")";
List<Object[]> paramsList = new ArrayList<Object[]>();
for (StopWatch watch : watches) {
watch.setMonitorId(id);
map = Utils.toMap(watch);
List<Object> params = new ArrayList<>();
for (String key : keySet) {
if (map.containsKey(key)) {
params.add(map.get(key));
} else {
params.add(null);
}
}
paramsList.add(params.toArray());
}
jdbcTemplate.batchUpdate(sql2, paramsList);
}
}
use of doitincloud.rdbcache.models.StopWatch in project rdbcache by rdbcache.
the class MonitorRepoImpl method findById.
@Override
public Monitor findById(Long id) {
String sql = "select * from " + monitorTable + " where id = ?";
Object[] params = new Object[] { id };
Monitor monitor = jdbcTemplate.queryForObject(sql, params, Monitor.class);
if (monitor == null) {
return null;
}
sql = "select * from " + stopWatchTable + " where monitor_id = ?";
List<StopWatch> list = jdbcTemplate.queryForList(sql, params, StopWatch.class);
monitor.setStopWatches(list);
return monitor;
}
use of doitincloud.rdbcache.models.StopWatch in project rdbcache by rdbcache.
the class AsyncOps method deleteKvPairKeyInfo.
public void deleteKvPairKeyInfo(Context context, KvPair pair, KeyInfo keyInfo) {
String kvType = "keyInfo";
String type = pair.getType();
if (!type.equals("data")) {
kvType += ":" + type;
}
KvPair toDeletePair = new KvPair(pair.getId(), kvType);
StopWatch stopWatch = context.startStopWatch("dbase", "KvPairRepo.delete");
AppCtx.getKvPairRepo().delete(toDeletePair);
if (stopWatch != null)
stopWatch.stopNow();
}
Aggregations