io.symcpe.wraith.silo.redis.RedisAggregationStore Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of wraith-silo Show documentation
Show all versions of wraith-silo Show documentation
Wriath state persistence library
/**
* Copyright 2016 Symantec Corporation.
*
* Licensed under the Apache License, Version 2.0 (the “License”);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.symcpe.wraith.silo.redis;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.clearspring.analytics.stream.cardinality.ICardinality;
import io.symcpe.wraith.MutableBoolean;
import io.symcpe.wraith.Utils;
import io.symcpe.wraith.aggregators.Aggregator;
import io.symcpe.wraith.aggregators.CoarseCountingAggregator;
import io.symcpe.wraith.aggregators.FineCountingAggregator;
import io.symcpe.wraith.aggregators.SetAggregator;
import io.symcpe.wraith.store.AggregationStore;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisSentinelPool;
/**
* @author ambud_sharma
*/
public class RedisAggregationStore implements AggregationStore {
private static final String PREFIX_STATES = "states_";
private static final String DEFAULT_REDIS_PORT = "6379";
private static final String DEFAULT_SENTINEL_PORT = "26379";
private static final String ASTORE_REDIS_PORT = "astore.redis.port";
private static final String ASTORE_REDIS_HOST = "astore.redis.host";
private static final String ASTORE_REDIS_CLUSTER_NAME = "astore.redis.clusterName";
private static final String ASTORE_REDIS_SENTINEL = "astore.redis.sentinel";
private static final Logger logger = LoggerFactory.getLogger(RedisAggregationStore.class);
private JedisSentinelPool sentinel;
private Jedis redis;
private boolean isSentinel;
private String masterName;
private String host;
private int port;
@Override
public void initialize(Map conf) {
this.isSentinel = Boolean.parseBoolean(conf.getOrDefault(ASTORE_REDIS_SENTINEL, "false").toString());
this.masterName = isSentinel ? conf.get(ASTORE_REDIS_CLUSTER_NAME).toString() : null;
this.host = conf.get(ASTORE_REDIS_HOST).toString();
this.port = Integer.parseInt(conf
.getOrDefault(ASTORE_REDIS_PORT, isSentinel ? DEFAULT_SENTINEL_PORT : DEFAULT_REDIS_PORT).toString());
}
@Override
public void connect() throws IOException {
if (isSentinel) {
String[] vals = host.split(",");
for (int i = 0; i < vals.length; i++) {
vals[i] = vals[i] + ":" + port;
}
logger.info("Attempting to connect to Redis sentinels:" + Arrays.toString(vals));
sentinel = new JedisSentinelPool(masterName, new HashSet<>(Arrays.asList(vals)));
} else {
redis = new Jedis(host, port);
}
logger.info("Successfully connected to Redis");
}
@Override
public void disconnect() throws IOException {
if (isSentinel && sentinel != null) {
sentinel.close();
} else {
redis.close();
}
}
@Override
public void putValue(int taskId, long timestamp, String entity, long count) {
if (isSentinel) {
redis = sentinel.getResource();
}
redis.set(buildAggregationKey(taskId, timestamp, entity), String.valueOf(count));
}
@Override
public void putValue(int taskId, long timestamp, String entity, int count) {
putValue(taskId, timestamp, entity, (long) count);
}
public static String buildAggregationKey(int taskId, long timestamp, String entity) {
return prefixAggregation(taskId) + "_" + Utils.longToString(timestamp);
}
public static String prefixAggregation(int taskId) {
return "agg_" + taskId + "_";
}
@SuppressWarnings("unchecked")
@Override
public void persist(int taskId, String entity, Aggregator aggregator) throws IOException {
if (isSentinel) {
redis = sentinel.getResource();
}
if (aggregator.getClass() == SetAggregator.class || aggregator.getClass() == FineCountingAggregator.class) {
mergeSetValues(taskId, entity, (Set