|
| 1 | +/* |
| 2 | + * Copyright (c) 2017 Mindstix Inc. |
| 3 | + */ |
| 4 | +package com.mindstix.cb.utils; |
| 5 | + |
| 6 | +import java.util.List; |
| 7 | + |
| 8 | +import org.slf4j.Logger; |
| 9 | +import org.slf4j.LoggerFactory; |
| 10 | + |
| 11 | +import redis.clients.jedis.Jedis; |
| 12 | +import redis.clients.jedis.JedisPool; |
| 13 | +import redis.clients.jedis.Transaction; |
| 14 | + |
| 15 | +/** |
| 16 | + * This utility handles all the Redis database and Jedis related operations. |
| 17 | + * Following operations are handled by RedisUtility.java |
| 18 | + * <ol> |
| 19 | + * <li>Distributed coordination for running tests with specific set of users. |
| 20 | + * </li> |
| 21 | + * <li>Acting as a temporary storage for report data and notification stages in |
| 22 | + * Jenkins pipeline</li> |
| 23 | + * </ol> |
| 24 | + * Please refer following URLs for more information about Redis and Jedis |
| 25 | + * <ol> |
| 26 | + * <li><a href="https://redis.io/commands">Redis Commands</a></li> |
| 27 | + * <li><a href="http://www.baeldung.com/jedis-java-redis-client-library"> Jedis: |
| 28 | + * java redis client library</a></li> |
| 29 | + * </ol> |
| 30 | + * |
| 31 | + * @author Mindstix |
| 32 | + */ |
| 33 | +public final class RedisUtility { |
| 34 | + |
| 35 | +private final static Logger LOGGER = LoggerFactory.getLogger(RedisUtility.class); |
| 36 | + |
| 37 | +private static JedisPool pool; |
| 38 | +private static Jedis jedis; |
| 39 | + |
| 40 | +private static boolean isRedisAlive; |
| 41 | + |
| 42 | +/** |
| 43 | + * private constructor |
| 44 | + */ |
| 45 | +private RedisUtility() { |
| 46 | +} |
| 47 | + |
| 48 | +static { |
| 49 | +configureJedisPool(); |
| 50 | +} |
| 51 | + |
| 52 | +/** |
| 53 | + * This method is used to configure Jedis pool. It configures the host and |
| 54 | + * port of Jedis pool. It takes redisHost from command line. If not passed from command line |
| 55 | + * it takes localhost as default redis host. You can change the default redisHost in build.gradle. |
| 56 | + * For remaining Redis configurations please @see Constants.java |
| 57 | + * |
| 58 | + */ |
| 59 | +private static void configureJedisPool() { |
| 60 | +try { |
| 61 | +LOGGER.info("Configuring jedis pool"); |
| 62 | +pool = new JedisPool(System.getProperty("env.redisHost"), Constants.REDIS_PORT); |
| 63 | +jedis = pool.getResource(); |
| 64 | +isRedisAlive = true; |
| 65 | +} catch (Exception e) { |
| 66 | +LOGGER.error("Failed to configure redis", e); |
| 67 | +isRedisAlive = false; |
| 68 | +} |
| 69 | +} |
| 70 | + |
| 71 | +/** |
| 72 | + * This method is used to acquire username which are stored in Redis set. If |
| 73 | + * usernames are available in set then it randomly pops one user else it |
| 74 | + * waits for 10 minutes for user to be available. If after 10 minutes user |
| 75 | + * is not available then it throws RunTimeException. It takes set-key as |
| 76 | + * parameter |
| 77 | + * |
| 78 | + * @param userKey |
| 79 | + * @return userName |
| 80 | + */ |
| 81 | +public static String acquireUser(String userKey) { |
| 82 | +if (!isRedisAlive) { |
| 83 | +String userName = Constants.FALLBACK_USER; |
| 84 | +LOGGER.warn("Returning fallback user {} as jedis is not configured", userName); |
| 85 | +return userName; |
| 86 | +} |
| 87 | + |
| 88 | +LOGGER.info("Acquiring user"); |
| 89 | +String userName = null; |
| 90 | +List<Object> result = null; |
| 91 | +Transaction transaction; |
| 92 | +for (int i = 0; i < Constants.WAIT_TIMEOUT_IN_SEC; i++) { |
| 93 | +transaction = jedis.multi(); |
| 94 | +transaction.spop(userKey); |
| 95 | +result = transaction.exec(); |
| 96 | +if (result != null) { |
| 97 | +break; |
| 98 | +} |
| 99 | +try { |
| 100 | +Thread.sleep(1000); |
| 101 | +} catch (InterruptedException e) { |
| 102 | +LOGGER.error("InterruptedException", e); |
| 103 | +} |
| 104 | +} |
| 105 | +if (result == null) { |
| 106 | +throw new RuntimeException("Time limit exceeded to acquire the user"); |
| 107 | +} else { |
| 108 | +userName = result.get(0).toString(); |
| 109 | +} |
| 110 | +LOGGER.info("User {} acquired", userName); |
| 111 | +return userName; |
| 112 | +} |
| 113 | + |
| 114 | +/** |
| 115 | + * This method is used to add or release user back to redis set. It takes |
| 116 | + * userKey and username as parameter. |
| 117 | + * |
| 118 | + * @param userKey |
| 119 | + * @param userName |
| 120 | + */ |
| 121 | +public static void releaseUser(String userKey, String userName) { |
| 122 | +if (userName != null && isRedisAlive) { |
| 123 | +LOGGER.info("Releasing user {}.", userName); |
| 124 | +Transaction transaction = jedis.multi(); |
| 125 | +transaction.sadd(userKey, userName); |
| 126 | +transaction.exec(); |
| 127 | +LOGGER.info("User {} released.", userName); |
| 128 | +} |
| 129 | +} |
| 130 | + |
| 131 | +} |
0 commit comments