
com.netflix.ndbench.plugin.dyno.DynoJedisUtils Maven / Gradle / Ivy
/**
* Copyright 2016 Netflix, Inc.
*
* 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 com.netflix.ndbench.plugin.dyno;
import com.netflix.dyno.jedis.DynoJedisClient;
import com.netflix.dyno.jedis.DynoJedisPipeline;
import com.netflix.ndbench.api.plugin.DataGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Response;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;
public class DynoJedisUtils {
// private final AtomicReference jedisClient = new
// AtomicReference(null);
private AtomicReference jedisClient;
private static final String ResultOK = "Ok";
private static final String CacheMiss = null;
private final static Logger logger = LoggerFactory.getLogger(DynoJedisUtils.class);
private static Random randomGenerator = new Random();
public DynoJedisUtils(AtomicReference jedisClient) {
this.jedisClient = jedisClient;
}
/**
* This is the non pipelined version of the reads
*
* @param key
* @return the value of the corresponding key
* @throws Exception
*/
public String nonPipelineRead(String key) throws Exception {
String res = jedisClient.get().get(key);
if (res != null) {
if (res.isEmpty()) {
throw new Exception("Data retrieved is not ok ");
}
} else {
return CacheMiss;
}
return ResultOK;
}
/**
* This is the pipelined version of the reads
*
* @param key
* @return "OK" if everything was read
* @throws Exception
*/
public String pipelineRead(String key, int max_pipe_keys, int min_pipe_keys) throws Exception {
int pipe_keys = randomGenerator.nextInt(max_pipe_keys);
pipe_keys = Math.max(min_pipe_keys, pipe_keys);
DynoJedisPipeline pipeline = this.jedisClient.get().pipelined();
Map> responses = new HashMap>();
for (int n = 0; n < pipe_keys; ++n) {
String nth_key = key + "_" + n;
// NOTE: Dyno Jedis works on only one key, so we always use the same
// key in every get operation
Response resp = pipeline.get(key);
// We however use the nth key as the key in the hashmap to check
// individual response on every operation.
responses.put(nth_key, resp);
}
pipeline.sync();
for (int n = 0; n < pipe_keys; ++n) {
String nth_key = key + "_" + n;
Response resp = responses.get(nth_key);
if (resp == null || resp.get() == null) {
logger.info("Cache Miss on pipelined read: key:" + key);
return null;
} else {
if (resp.get().startsWith("ERR")) {
throw new Exception(String.format("DynoJedisPipeline: error %s", resp.get()));
}
if (!isValidResponse(key, resp.get())) {
throw new Exception(String.format(
"DynoJedisPipeline: pipeline read: value %s does not contain key %s", resp.get(), key));
}
}
}
return "OK";
}
/**
* This the pipelined HGETALL
*
* @param key
* @return the contents of the hash
* @throws Exception
*/
public String pipelineReadHGETALL(String key, String hm_key_prefix) throws Exception {
DynoJedisPipeline pipeline = jedisClient.get().pipelined();
Response
© 2015 - 2025 Weber Informatics LLC | Privacy Policy