private List<String> getCurrentServerIds(boolean nag, boolean lagged) { try (Jedis jedis = pool.getResource()) { long time = getRedisTime(jedis.time()); int nagTime = 0; if (nag) { nagTime = nagAboutServers.decrementAndGet(); if (nagTime <= 0) { nagAboutServers.set(10); } } ImmutableList.Builder<String> servers = ImmutableList.builder(); Map<String, String> heartbeats = jedis.hgetAll("heartbeats"); for (Map.Entry<String, String> entry : heartbeats.entrySet()) { try { long stamp = Long.parseLong(entry.getValue()); if (lagged ? time >= stamp + 30 : time <= stamp + 30) servers.add(entry.getKey()); else if (nag && nagTime <= 0) { getLogger() .severe( entry.getKey() + " is " + (time - stamp) + " seconds behind! (Time not synchronized or server down?)"); } } catch (NumberFormatException ignored) { } } return servers.build(); } catch (JedisConnectionException e) { getLogger().log(Level.SEVERE, "Unable to fetch server IDs", e); return Collections.singletonList(configuration.getServerId()); } }
private void loadConfig() throws IOException, JedisConnectionException { if (!getDataFolder().exists()) { getDataFolder().mkdir(); } File file = new File(getDataFolder(), "config.yml"); if (!file.exists()) { file.createNewFile(); try (InputStream in = getResourceAsStream("example_config.yml"); OutputStream out = new FileOutputStream(file)) { ByteStreams.copy(in, out); } } final Configuration configuration = ConfigurationProvider.getProvider(YamlConfiguration.class).load(file); final String redisServer = configuration.getString("redis-server", "localhost"); final int redisPort = configuration.getInt("redis-port", 6379); String redisPassword = configuration.getString("redis-password"); String serverId = configuration.getString("server-id"); if (redisPassword != null && (redisPassword.isEmpty() || redisPassword.equals("none"))) { redisPassword = null; } // Configuration sanity checks. if (serverId == null || serverId.isEmpty()) { throw new RuntimeException("server-id is not specified in the configuration or is empty"); } if (redisServer != null && !redisServer.isEmpty()) { final String finalRedisPassword = redisPassword; FutureTask<JedisPool> task = new FutureTask<>( new Callable<JedisPool>() { @Override public JedisPool call() throws Exception { // With recent versions of Jedis, we must set the classloader to the one // BungeeCord used // to load RedisBungee with. ClassLoader previous = Thread.currentThread().getContextClassLoader(); Thread.currentThread().setContextClassLoader(RedisBungee.class.getClassLoader()); // Create the pool... JedisPoolConfig config = new JedisPoolConfig(); config.setMaxTotal(configuration.getInt("max-redis-connections", 8)); JedisPool pool = new JedisPool(config, redisServer, redisPort, 0, finalRedisPassword); // Reset classloader and return the pool Thread.currentThread().setContextClassLoader(previous); return pool; } }); getProxy().getScheduler().runAsync(this, task); try { pool = task.get(); } catch (InterruptedException | ExecutionException e) { throw new RuntimeException("Unable to create Redis pool", e); } // Test the connection try (Jedis rsc = pool.getResource()) { rsc.ping(); // If that worked, now we can check for an existing, alive Bungee: File crashFile = new File(getDataFolder(), "restarted_from_crash.txt"); if (crashFile.exists()) { crashFile.delete(); } else if (rsc.hexists("heartbeats", serverId)) { try { long value = Long.parseLong(rsc.hget("heartbeats", serverId)); if (System.currentTimeMillis() < value + 20000) { getLogger() .severe( "You have launched a possible impostor BungeeCord instance. Another instance is already running."); getLogger() .severe("For data consistency reasons, RedisBungee will now disable itself."); getLogger() .severe( "If this instance is coming up from a crash, create a file in your RedisBungee plugins directory with the name 'restarted_from_crash.txt' and RedisBungee will not perform this check."); throw new RuntimeException("Possible impostor instance!"); } } catch (NumberFormatException ignored) { } } FutureTask<Void> task2 = new FutureTask<>( new Callable<Void>() { @Override public Void call() throws Exception { httpClient = new OkHttpClient(); Dispatcher dispatcher = new Dispatcher(getExecutorService()); httpClient.setDispatcher(dispatcher); NameFetcher.setHttpClient(httpClient); UUIDFetcher.setHttpClient(httpClient); RedisBungee.configuration = new RedisBungeeConfiguration(RedisBungee.this.getPool(), configuration); return null; } }); getProxy().getScheduler().runAsync(this, task2); try { task2.get(); } catch (InterruptedException | ExecutionException e) { throw new RuntimeException("Unable to create HTTP client", e); } getLogger().log(Level.INFO, "Successfully connected to Redis."); } catch (JedisConnectionException e) { pool.destroy(); pool = null; throw e; } } else { throw new RuntimeException("No redis server specified!"); } }
final int getCurrentCount() { Long count = (Long) getPlayerCountScript.eval(ImmutableList.<String>of(), ImmutableList.<String>of()); return count.intValue(); }
private long getRedisTime(List<String> timeRes) { return Long.parseLong(timeRes.get(0)); }
/*WARNING: everything related to matlab should be taken carefully, except for return value from function method any other matlab array object such as from new should be disposed explicitly. And set method of matlab array normally copy the element especially the element is a matlab array! If these array are not explicitly disposed they will not be collected by GC either thus your memory will be drained very quickly!!*/ @Override public int runTask(String caseid, List<String> zoneids, String taskName) throws TException { int retcode = 0; String field = SeUtils.MW.WORKER.STRUCT_ARRAY_VALUE_FIELD; try (Jedis jedis = jedisPool.getResource()) { jedis.auth(pass); for (String zoneid : zoneids) { if (!jedis.exists(mkByteKey(caseid, SeUtils.REDIS.KEYS.ZONES, zoneid))) { retcode = -2; } } if (retcode >= 0) { Pipeline p = jedis.pipelined(); Response<String> tol = p.hget(mkKey(caseid, SeUtils.REDIS.KEYS.OPTIONS_EST), SeUtils.OPTIONS.KEYS.OPT_EST_TOL); p.sync(); double toldbl = Double.parseDouble(tol.get()); int nz = zoneids.size(); if (taskName.equals(SeUtils.MW.WORKER.ESTIMATE_TASK)) { List<Response<byte[]>> zoneDataByteList = new ArrayList<>(); List<Response<byte[]>> HHByteList = new ArrayList<>(); List<Response<byte[]>> WWInvByteList = new ArrayList<>(); List<Response<byte[]>> ddelzByteList = new ArrayList<>(); List<Response<byte[]>> vvByteList = new ArrayList<>(); List<Response<List<String>>> busIdsList = new ArrayList<>(); List<Response<List<String>>> outBusIdsList = new ArrayList<>(); List<Response<List<String>>> brIdsList = new ArrayList<>(); for (int i = 0; i < nz; i++) { String zoneid = zoneids.get(i); byte[] HHkey = mkByteKey(caseid, zoneid, SeUtils.REDIS.KEYS.STATE_HH); byte[] WWInvKey = mkByteKey(caseid, zoneid, SeUtils.REDIS.KEYS.STATE_WWINV); byte[] ddelzKey = mkByteKey(caseid, zoneid, SeUtils.REDIS.KEYS.STATE_DDELZ); byte[] vvKey = mkByteKey(caseid, zoneid, SeUtils.REDIS.KEYS.STATE, SeUtils.REDIS.KEYS.STATE_VV); byte[] zoneDataKey = mkByteKey(caseid, SeUtils.REDIS.KEYS.ZONES, zoneid); zoneDataByteList.add(p.get(zoneDataKey)); HHByteList.add(p.get(HHkey)); WWInvByteList.add(p.get(WWInvKey)); ddelzByteList.add(p.get(ddelzKey)); vvByteList.add(p.get(vvKey)); String busIdsKey = mkKey(caseid, SeUtils.REDIS.KEYS.ZONES, zoneid, SeUtils.REDIS.KEYS.BUS_NUM_OUT); String outBusIdsKey = mkKey(caseid, SeUtils.REDIS.KEYS.ZONES, zoneid, SeUtils.REDIS.KEYS.OUT_BUS_NUM_OUT); String brIdsKey = mkKey(caseid, SeUtils.REDIS.KEYS.ZONES, zoneid, SeUtils.REDIS.KEYS.BRANCH_IDS); busIdsList.add(p.lrange(busIdsKey, 0, -1)); outBusIdsList.add(p.lrange(outBusIdsKey, 0, -1)); brIdsList.add(p.lrange(brIdsKey, 0, -1)); } p.sync(); String[] fields = {field}; MWStructArray zonesData = new MWStructArray(nz, 1, fields); MWStructArray HHsData = new MWStructArray(nz, 1, fields); MWStructArray WWInvsData = new MWStructArray(nz, 1, fields); MWStructArray ddelzsData = new MWStructArray(nz, 1, fields); MWStructArray vvsData = new MWStructArray(nz, 1, fields); MWArray zoneMat, HHMat, WWInvMat, ddelzMat, vvMat; for (int i = 0; i < nz; i++) { zoneMat = MWStructArray.deserialize(zoneDataByteList.get(i).get()); zonesData.set(field, i + 1, zoneMat); HHMat = MWNumericArray.deserialize(HHByteList.get(i).get()); HHsData.set(field, i + 1, HHMat); WWInvMat = MWNumericArray.deserialize(WWInvByteList.get(i).get()); WWInvsData.set(field, i + 1, WWInvMat); ddelzMat = MWNumericArray.deserialize(ddelzByteList.get(i).get()); ddelzsData.set(field, i + 1, ddelzMat); vvMat = MWNumericArray.deserialize(vvByteList.get(i).get()); vvsData.set(field, i + 1, vvMat); disposeMatArrays(zoneMat, HHMat, WWInvMat, ddelzMat, vvMat); } // get measurement and voltages List<Response<List<String>>> vaEstList = new ArrayList<>(); List<Response<List<String>>> vmEstList = new ArrayList<>(); List<Response<List<String>>> vaExtList = new ArrayList<>(); List<Response<List<String>>> vmExtList = new ArrayList<>(); List<Response<List<String>>> zpfList = new ArrayList<>(); List<Response<List<String>>> zptList = new ArrayList<>(); List<Response<List<String>>> zqfList = new ArrayList<>(); List<Response<List<String>>> zqtList = new ArrayList<>(); List<Response<List<String>>> pbusList = new ArrayList<>(); List<Response<List<String>>> qbusList = new ArrayList<>(); List<Response<List<String>>> vamList = new ArrayList<>(); List<Response<List<String>>> vmmList = new ArrayList<>(); for (int i = 0; i < nz; i++) { List<String> busIdsLst = busIdsList.get(i).get(); List<String> outBusIdsLst = outBusIdsList.get(i).get(); List<String> bridsLst = brIdsList.get(i).get(); vaEstList.add( p.hmget( mkKey(caseid, SeUtils.REDIS.KEYS.VA_EST_HASH), busIdsLst.toArray(new String[busIdsLst.size()]))); vmEstList.add( p.hmget( mkKey(caseid, SeUtils.REDIS.KEYS.VM_EST_HASH), busIdsLst.toArray(new String[busIdsLst.size()]))); vaExtList.add( p.hmget( mkKey(caseid, SeUtils.REDIS.KEYS.VA_EST_HASH), outBusIdsLst.toArray(new String[outBusIdsLst.size()]))); vmExtList.add( p.hmget( mkKey(caseid, SeUtils.REDIS.KEYS.VM_EST_HASH), outBusIdsLst.toArray(new String[outBusIdsLst.size()]))); zpfList.add( p.hmget( mkKey(caseid, SeUtils.REDIS.KEYS.MEASURE, SeUtils.MEASURE.TYPE.PF), bridsLst.toArray(new String[bridsLst.size()]))); zptList.add( p.hmget( mkKey(caseid, SeUtils.REDIS.KEYS.MEASURE, SeUtils.MEASURE.TYPE.PT), bridsLst.toArray(new String[bridsLst.size()]))); zqfList.add( p.hmget( mkKey(caseid, SeUtils.REDIS.KEYS.MEASURE, SeUtils.MEASURE.TYPE.QF), bridsLst.toArray(new String[bridsLst.size()]))); zqtList.add( p.hmget( mkKey(caseid, SeUtils.REDIS.KEYS.MEASURE, SeUtils.MEASURE.TYPE.QT), bridsLst.toArray(new String[bridsLst.size()]))); pbusList.add( p.hmget( mkKey(caseid, SeUtils.REDIS.KEYS.MEASURE, SeUtils.MEASURE.TYPE.PBUS), busIdsLst.toArray(new String[busIdsLst.size()]))); qbusList.add( p.hmget( mkKey(caseid, SeUtils.REDIS.KEYS.MEASURE, SeUtils.MEASURE.TYPE.QBUS), busIdsLst.toArray(new String[busIdsLst.size()]))); vamList.add( p.hmget( mkKey(caseid, SeUtils.REDIS.KEYS.MEASURE, SeUtils.MEASURE.TYPE.VA), busIdsLst.toArray(new String[busIdsLst.size()]))); vmmList.add( p.hmget( mkKey(caseid, SeUtils.REDIS.KEYS.MEASURE, SeUtils.MEASURE.TYPE.VM), busIdsLst.toArray(new String[busIdsLst.size()]))); } p.sync(); MWStructArray vasEstData = new MWStructArray(nz, 1, fields); MWStructArray vmsEstData = new MWStructArray(nz, 1, fields); MWStructArray vasExtData = new MWStructArray(nz, 1, fields); MWStructArray vmsExtData = new MWStructArray(nz, 1, fields); MWStructArray zsData = new MWStructArray(nz, 1, fields); MWArray vaestMat, vmestMat, vaextMat, vmextMat, zMat; for (int i = 0; i < nz; i++) { vaestMat = new MWNumericArray(vaEstList.get(i).get().toArray(), MWClassID.DOUBLE); vmestMat = new MWNumericArray(vmEstList.get(i).get().toArray(), MWClassID.DOUBLE); vaextMat = new MWNumericArray(vaExtList.get(i).get().toArray(), MWClassID.DOUBLE); vmextMat = new MWNumericArray(vmExtList.get(i).get().toArray(), MWClassID.DOUBLE); vasEstData.set(field, i + 1, vaestMat); vmsEstData.set(field, i + 1, vmestMat); vasExtData.set(field, i + 1, vaextMat); vmsExtData.set(field, i + 1, vmextMat); List<String> z = new ArrayList<>(); z.addAll(zpfList.get(i).get()); z.addAll(zptList.get(i).get()); z.addAll(pbusList.get(i).get()); z.addAll(vamList.get(i).get()); z.addAll(zqfList.get(i).get()); z.addAll(zqtList.get(i).get()); z.addAll(qbusList.get(i).get()); z.addAll(vmmList.get(i).get()); zMat = new MWNumericArray(z.toArray(), MWClassID.DOUBLE); zsData.set(field, i + 1, zMat); disposeMatArrays(vaestMat, vmestMat, vaextMat, vmextMat, zMat); } Object[] res = null; MWStructArray delz = null, normF = null, ddelz = null, VVa = null, VVm = null, step = null, success = null; try { res = estimator.api_estimateOnce_batch( 7, HHsData, WWInvsData, ddelzsData, vvsData, vasEstData, vmsEstData, vasExtData, vmsExtData, zsData, zonesData); disposeMatArrays( zonesData, zsData, vasEstData, vmsEstData, vasExtData, vmsExtData, HHsData, WWInvsData, ddelzsData, vvsData); } catch (MWException e) { e.printStackTrace(); } if (res != null) { VVa = (MWStructArray) res[0]; VVm = (MWStructArray) res[1]; delz = (MWStructArray) res[2]; ddelz = (MWStructArray) res[3]; normF = (MWStructArray) res[4]; step = (MWStructArray) res[5]; success = (MWStructArray) res[6]; if (!isSuccess(success)) { logger.warn( "Case {} estimation not success, may be caused by too many bad data drops!", caseid); } for (int i = 0; i < nz; i++) { List<String> busids = busIdsList.get(i).get(); // update state MWNumericArray va = (MWNumericArray) VVa.getField(field, i + 1); MWNumericArray vm = (MWNumericArray) VVm.getField(field, i + 1); double[][] vaArr = (double[][]) va.toArray(); double[][] vmArr = (double[][]) vm.toArray(); Map<String, String> vaMap = new HashMap<>(); Map<String, String> vmMap = new HashMap<>(); for (int j = 0; j < busids.size(); j++) { vaMap.put(busids.get(j), String.valueOf(vaArr[j][0])); vmMap.put(busids.get(j), String.valueOf(vmArr[j][0])); } p.hmset(mkKey(caseid, SeUtils.REDIS.KEYS.VA_EST_BUFFER_HASH), vaMap); p.hmset(mkKey(caseid, SeUtils.REDIS.KEYS.VM_EST_BUFFER_HASH), vmMap); String zoneid = zoneids.get(i); MWNumericArray ddelzi = (MWNumericArray) ddelz.getField(field, i + 1); MWNumericArray delzi = (MWNumericArray) delz.getField(field, i + 1); p.set(mkByteKey(caseid, zoneid, SeUtils.REDIS.KEYS.STATE_DDELZ), ddelzi.serialize()); p.set( mkByteKey( caseid, zoneid, SeUtils.REDIS.KEYS.STATE, SeUtils.REDIS.KEYS.STATE_DELZ), delzi.serialize()); // estimated zones p.incr(mkKey(caseid, SeUtils.REDIS.KEYS.STATE_ESTIMATED_ZONES)); // iteration times // TODO: record only one iteration number p.incr(mkKey(caseid, zoneid, SeUtils.REDIS.KEYS.STATE, SeUtils.REDIS.KEYS.STATE_IT)); MWNumericArray stepimat = (MWNumericArray) step.getField(field, i + 1); double stepi = stepimat.getDouble(); if (stepi < toldbl) { p.setbit( mkKey(caseid, SeUtils.REDIS.KEYS.STATE_CONVERGED), Long.parseLong(zoneid), true); } else { p.setbit( mkKey(caseid, SeUtils.REDIS.KEYS.STATE_CONVERGED), Long.parseLong(zoneid), false); } disposeMatArrays(va, vm, ddelzi, delzi, stepimat); // debug // System.out.println("zone: " + zoneid + "; step:" + stepi); } p.sync(); disposeMatArrays(VVa, VVm, delz, ddelz, normF, success, step); } } else if (taskName.equals(SeUtils.MW.WORKER.BADRECOG_TASK)) { List<Response<byte[]>> WWByteList = new ArrayList<>(); List<Response<byte[]>> HHByteList = new ArrayList<>(); List<Response<byte[]>> WWInvByteList = new ArrayList<>(); List<Response<byte[]>> ddelzByteList = new ArrayList<>(); List<Response<byte[]>> vvByteList = new ArrayList<>(); List<Response<String>> badthdList = new ArrayList<>(); for (int i = 0; i < nz; i++) { String zoneid = zoneids.get(i); byte[] HHkey = mkByteKey(caseid, zoneid, SeUtils.REDIS.KEYS.STATE_HH); byte[] WWInvKey = mkByteKey(caseid, zoneid, SeUtils.REDIS.KEYS.STATE_WWINV); byte[] ddelzKey = mkByteKey(caseid, zoneid, SeUtils.REDIS.KEYS.STATE_DDELZ); byte[] vvKey = mkByteKey(caseid, zoneid, SeUtils.REDIS.KEYS.STATE, SeUtils.REDIS.KEYS.STATE_VV); byte[] WWKey = mkByteKey(caseid, zoneid, SeUtils.REDIS.KEYS.STATE_WW); WWByteList.add(p.get(WWKey)); HHByteList.add(p.get(HHkey)); WWInvByteList.add(p.get(WWInvKey)); ddelzByteList.add(p.get(ddelzKey)); vvByteList.add(p.get(vvKey)); String thdKey = mkKey( caseid, SeUtils.REDIS.KEYS.ZONES, zoneid, SeUtils.REDIS.KEYS.BAD_RECOG_THRESHOLD); badthdList.add(p.get(thdKey)); } p.sync(); String[] fields = {field}; MWStructArray WWsData = new MWStructArray(nz, 1, fields); MWStructArray HHsData = new MWStructArray(nz, 1, fields); MWStructArray WWInvsData = new MWStructArray(nz, 1, fields); MWStructArray ddelzsData = new MWStructArray(nz, 1, fields); MWStructArray vvsData = new MWStructArray(nz, 1, fields); MWStructArray badthdsData = new MWStructArray(nz, 1, fields); MWArray wwmat, hhmat, wwinvmat, ddelzmat, vvmat, badmat; for (int i = 0; i < nz; i++) { wwmat = MWNumericArray.deserialize(WWByteList.get(i).get()); hhmat = MWNumericArray.deserialize(HHByteList.get(i).get()); wwinvmat = MWNumericArray.deserialize(WWInvByteList.get(i).get()); ddelzmat = MWNumericArray.deserialize(ddelzByteList.get(i).get()); vvmat = MWNumericArray.deserialize(vvByteList.get(i).get()); badmat = new MWNumericArray(badthdList.get(i).get(), MWClassID.DOUBLE); WWsData.set(field, i + 1, wwmat); HHsData.set(field, i + 1, hhmat); WWInvsData.set(field, i + 1, wwinvmat); ddelzsData.set(field, i + 1, ddelzmat); vvsData.set(field, i + 1, vvmat); badthdsData.set(field, i + 1, badmat); disposeMatArrays(wwmat, hhmat, wwinvmat, ddelzmat, vvmat, badmat); } MWStructArray vvNewMat = null, convergedMat = null; Object[] res = null; try { res = estimator.api_badDataRecognition_batch( 2, HHsData, WWsData, WWInvsData, vvsData, ddelzsData, badthdsData); disposeMatArrays(badthdsData, HHsData, WWInvsData, WWsData, ddelzsData); } catch (MWException e) { e.printStackTrace(); } // update state if (res != null) { vvNewMat = (MWStructArray) res[0]; convergedMat = (MWStructArray) res[1]; for (int i = 0; i < nz; i++) { byte[] vvKey = mkByteKey( caseid, zoneids.get(i), SeUtils.REDIS.KEYS.STATE, SeUtils.REDIS.KEYS.STATE_VV); String convKey = mkKey(caseid, SeUtils.REDIS.KEYS.STATE_CONVERGED); MWNumericArray vviMat = (MWNumericArray) vvsData.getField(field, i + 1); MWNumericArray vvNewiMat = (MWNumericArray) vvNewMat.getField(field, i + 1); int nvvi = vviMat.getDimensions()[0]; int nvvNewi = vvNewiMat.getDimensions()[0]; if (nvvi != nvvNewi) { p.set(vvKey, vvNewiMat.serialize()); } MWNumericArray convergediMat = (MWNumericArray) convergedMat.getField(field, i + 1); double converged = convergediMat.getDouble(); if (!(converged < 0)) { p.setbit(convKey, Long.parseLong(zoneids.get(i)), false); } p.incr(mkKey(caseid, SeUtils.REDIS.KEYS.STATE_BADRECOG_ZONES)); p.incr( mkKey( caseid, zoneids.get(i), SeUtils.REDIS.KEYS.STATE, SeUtils.REDIS.KEYS.STATE_IBADREG)); disposeMatArrays(vviMat, vvNewiMat, convergediMat); } p.sync(); disposeMatArrays(vvsData, vvNewMat, convergedMat); } } else if (taskName.equals(testTask)) { p.set(testTask, System.currentTimeMillis() + ""); p.sync(); retcode = 10; } else { retcode = -3; } } } return retcode; }