示例#1
0
def test_rename_dst():

    env = Env()
    with env.getClusterConnectionIfNeeded() as r:

        assert r.execute_command('TS.CREATE', 'a{2}')
        assert r.execute_command('TS.CREATE', 'b{2}')
        assert r.execute_command('TS.CREATERULE', 'a{2}', 'b{2}',
                                 'AGGREGATION', 'AVG', 5000)

        env.assertTrue(r.execute_command('RENAME', 'b{2}', 'b1{2}'))
        aInfo = TSInfo(r.execute_command('TS.INFO', 'a{2}'))
        env.assertEqual(aInfo.sourceKey, None)
        env.assertEqual(aInfo.rules[0][0], b'b1{2}')

        assert r.execute_command('TS.CREATE', 'c{2}')
        assert r.execute_command('TS.CREATERULE', 'a{2}', 'c{2}',
                                 'AGGREGATION', 'COUNT', 2000)

        assert r.execute_command('TS.CREATE', 'd{2}')
        assert r.execute_command('TS.CREATERULE', 'a{2}', 'd{2}',
                                 'AGGREGATION', 'SUM', 3000)

        env.assertTrue(r.execute_command('RENAME', 'c{2}', 'c1{2}'))
        aInfo = TSInfo(r.execute_command('TS.INFO', 'a{2}'))
        env.assertEqual(aInfo.sourceKey, None)
        env.assertEqual(aInfo.rules[0][0], b'b1{2}')
        env.assertEqual(aInfo.rules[1][0], b'c1{2}')
        env.assertEqual(aInfo.rules[2][0], b'd{2}')
示例#2
0
def test_groupby_reduce_multiple_groups():
    env = Env()
    with env.getClusterConnectionIfNeeded() as r:
        assert r.execute_command('TS.ADD', 's1', 1, 100, 'LABELS', 'HOST', 'A', 'REGION', 'EU', 'PROVIDER', 'AWS')
        assert r.execute_command('TS.ADD', 's2', 1, 55, 'LABELS', 'HOST', 'B', 'REGION', 'EU', 'PROVIDER', 'AWS')
        assert r.execute_command('TS.ADD', 's2', 2, 90, 'LABELS', 'HOST', 'B', 'REGION', 'EU', 'PROVIDER', 'AWS')
        assert r.execute_command('TS.ADD', 's3', 2, 40, 'LABELS', 'HOST', 'C', 'REGION', 'US', 'PROVIDER', 'AWS')

        actual_result = r.execute_command(
            'TS.mrange', '-', '+', 'WITHLABELS', 'FILTER', 'PROVIDER=AWS', 'GROUPBY', 'REGION', 'REDUCE', 'max')
        serie1 = actual_result[0]
        serie1_name = serie1[0]
        serie1_labels = serie1[1]
        serie1_values = serie1[2]
        env.assertEqual(serie1_values, [[1, b'100'],[2, b'90']])
        env.assertEqual(serie1_name, b'REGION=EU')
        env.assertEqual(serie1_labels[0][0], b'REGION')
        env.assertEqual(serie1_labels[0][1], b'EU')
        env.assertEqual(serie1_labels[1][0], b'__reducer__')
        env.assertEqual(serie1_labels[1][1], b'max')
        env.assertEqual(serie1_labels[2][0], b'__source__')
        env.assertEqual(sorted(serie1_labels[2][1].decode("ascii").split(",")), ['s1', 's2'])
        serie2 = actual_result[1]
        serie2_name = serie2[0]
        serie2_labels = serie2[1]
        serie2_values = serie2[2]
        env.assertEqual(serie2_values, [[2, b'40']])
        env.assertEqual(serie2_name, b'REGION=US')
        env.assertEqual(serie2_labels[0][0], b'REGION')
        env.assertEqual(serie2_labels[0][1], b'US')
        env.assertEqual(serie2_labels[1][0], b'__reducer__')
        env.assertEqual(serie2_labels[1][1], b'max')
        env.assertEqual(serie2_labels[2][0], b'__source__')
        env.assertEqual(serie2_labels[2][1], b's3')
示例#3
0
def test_evict():
    env = Env()
    env.skipOnCluster()
    skip_on_rlec()
    with env.getClusterConnectionIfNeeded() as r:
        info = r.execute_command('INFO')
        max_mem = info['used_memory'] + 1024 * 1024
        assert r.execute_command('CONFIG', 'SET', 'maxmemory',
                                 str(max_mem) + 'b')
        assert r.execute_command('CONFIG', 'SET', 'maxmemory-policy',
                                 'allkeys-lru')
        init(env, r)

        # make sure t{1} deleted
        res = r.execute_command('keys *')
        i = 4
        while b't{1}' in res:
            assert r.execute_command('TS.CREATE', 't{%s}' % (i, ))
            i += 1
            res = r.execute_command('keys *')

        res = r.execute_command('TS.QUERYINDEX', 'name=(mush)')
        env.assertEqual(res, [])

        # restore maxmemory
        assert r.execute_command('CONFIG', 'SET', 'maxmemory', '0')
def test_non_local_filtered_data():
    env = Env()
    with env.getClusterConnectionIfNeeded() as r:
        r.execute_command('TS.ADD', '{host1}_metric_1', 1, 100, 'LABELS',
                          'metric', 'cpu')
        r.execute_command('TS.ADD', '{host1}_metric_2', 2, 40, 'LABELS',
                          'metric', 'cpu')
        r.execute_command('TS.ADD', '{host1}_metric_1', 2, 95)
        r.execute_command('TS.ADD', '{host1}_metric_1', 10, 99)

    previous_results = []
    # ensure that initiating the query on different shards always replies with the same series
    for shard in range(0, env.shardsCount):
        shard_conn = env.getConnection(shard)
        # send undordered timestamps to test for sorting
        actual_result = shard_conn.execute_command(
            'TS.MRANGE - + FILTER_BY_TS 11 5 25 55 101 18 9 1900 2 FILTER metric=cpu'
        )
        env.assertEqual(len(actual_result), 2)

        # ensure reply is properly filtered by TS
        for serie in actual_result:
            serie_ts = serie[2]
            # ensure only timestamp 2 is present on reply
            env.assertEqual(len(serie_ts), 1)
            env.assertEqual(serie_ts[0][0], 2)

        for previous_result in previous_results:
            ensure_replies_series_match(env, previous_result, actual_result)
        previous_results.append(actual_result)
def test_groupby_reduce_errors():
    env = Env()
    with env.getClusterConnectionIfNeeded() as r:
        assert r.execute_command('TS.ADD', 's1', 1, 100, 'LABELS',
                                 'metric_family', 'cpu', 'metric_name', 'user')
        assert r.execute_command('TS.ADD', 's2', 2, 55, 'LABELS',
                                 'metric_family', 'cpu', 'metric_name', 'user')
        assert r.execute_command('TS.ADD', 's3', 2, 40, 'LABELS',
                                 'metric_family', 'cpu', 'metric_name',
                                 'system')
        assert r.execute_command('TS.ADD', 's1', 2, 95)

        # test wrong arity
        with pytest.raises(redis.ResponseError) as excinfo:
            assert r.execute_command('TS.mrange', '-', '+', 'WITHLABELS',
                                     'FILTER', 'metric_family=cpu', 'GROUPBY')

        with pytest.raises(redis.ResponseError) as excinfo:
            assert r.execute_command('TS.mrange', '-', '+', 'WITHLABELS',
                                     'FILTER', 'metric_family=cpu', 'GROUPBY',
                                     'metric_name')

        with pytest.raises(redis.ResponseError) as excinfo:
            assert r.execute_command('TS.mrange', '-', '+', 'WITHLABELS',
                                     'FILTER', 'metric_family=cpu', 'GROUPBY',
                                     'metric_name', 'abc', 'abc')

        with pytest.raises(redis.ResponseError) as excinfo:
            assert r.execute_command('TS.mrange', '-', '+', 'WITHLABELS',
                                     'FILTER', 'metric_family=cpu', 'GROUPBY',
                                     'metric_name', 'REDUCE', 'bla')
示例#6
0
def test_unlink():
    env = Env()
    with env.getClusterConnectionIfNeeded() as r:
        init(env, r)

        res = r.execute_command('UNLINK', 't{1}')
        res = r.execute_command('TS.QUERYINDEX', 'name=(mush,zavi,rex)')
        env.assertEqual(sorted(res), sorted([b't{2}', b't{1}_agg']))
        res = r.execute_command('TS.MGET', 'filter', 'name=(mush,zavi,rex)')
        env.assertEqual(sorted(res),
                        sorted([[b't{2}', [], []], [b't{1}_agg', [], []]]))
示例#7
0
def test_groupby_reduce_empty():
    env = Env()
    with env.getClusterConnectionIfNeeded() as r:
        assert r.execute_command('TS.ADD', 's1', 1, 100, 'LABELS', 'metric_family', 'cpu', 'metric_name', 'user')
        assert r.execute_command('TS.ADD', 's2', 2, 55, 'LABELS', 'metric_family', 'cpu', 'metric_name', 'user')
        assert r.execute_command('TS.ADD', 's3', 2, 40, 'LABELS', 'metric_family', 'cpu', 'metric_name', 'system')
        assert r.execute_command('TS.ADD', 's1', 2, 95)

        actual_result = r.execute_command(
            'TS.mrange', '-', '+', 'WITHLABELS', 'FILTER', 'metric_family=cpu', 'GROUPBY', 'labelX', 'REDUCE', 'max')
        env.assertEqual(actual_result, [])
示例#8
0
def test_rename_indexed():

    env = Env()
    with env.getClusterConnectionIfNeeded() as r:
        
        assert r.execute_command('TS.ADD', 'a{3}', 100, 200, 'LABELS', 'sensor_id', '2', 'area_id', '32')
        env.assertEqual(r.execute_command('TS.MGET', 'FILTER', 'area_id=32'), [[b'a{3}', [], [100, b'200']]])

        env.assertTrue(r.execute_command('RENAME', 'a{3}', 'a1{3}'))

        env.assertEqual(r.execute_command('TS.MGET', 'FILTER', 'area_id=32'), [[b'a1{3}', [], [100, b'200']]])
示例#9
0
def test_flush():
    env = Env()
    with env.getClusterConnectionIfNeeded() as r:
        init(env, r)

        assert r.execute_command('FLUSHALL')

        res = r.execute_command('TS.QUERYINDEX', 'name=(mush,zavi,rex)')
        env.assertEqual(res, [])

        init(env, r)
        assert r.execute_command('FLUSHDB')
        res = r.execute_command('TS.QUERYINDEX', 'name=(mush,zavi,rex)')
        env.assertEqual(res, [])
示例#10
0
def test_groupby_reduce():
    env = Env()
    with env.getClusterConnectionIfNeeded() as r:
        assert r.execute_command('TS.ADD', 's1', 1, 100, 'LABELS', 'metric_family', 'cpu', 'metric_name', 'user')
        assert r.execute_command('TS.ADD', 's2', 2, 55, 'LABELS', 'metric_family', 'cpu', 'metric_name', 'user')
        assert r.execute_command('TS.ADD', 's3', 2, 40, 'LABELS', 'metric_family', 'cpu', 'metric_name', 'system')
        assert r.execute_command('TS.ADD', 's1', 2, 95)

        actual_result = r.execute_command(
            'TS.mrange', '-', '+', 'WITHLABELS', 'FILTER', 'metric_family=cpu', 'GROUPBY', 'metric_name', 'REDUCE', 'max')
        serie1 = actual_result[0]
        serie1_name = serie1[0]
        serie1_labels = serie1[1]
        serie1_values = serie1[2]
        env.assertEqual(serie1_values, [[2, b'40']])
        env.assertEqual(serie1_name, b'metric_name=system')
        env.assertEqual(serie1_labels[0][0], b'metric_name')
        env.assertEqual(serie1_labels[0][1], b'system')
        serie2 = actual_result[1]
        serie2_name = serie2[0]
        serie2_labels = serie2[1]
        serie2_values = serie2[2]
        env.assertEqual(serie2_name, b'metric_name=user')
        env.assertEqual(serie2_labels[0][0], b'metric_name')
        env.assertEqual(serie2_labels[0][1], b'user')
        env.assertEqual(serie2_labels[1][0], b'__reducer__')
        env.assertEqual(serie2_labels[1][1], b'max')
        env.assertEqual(serie2_labels[2][0], b'__source__')
        env.assertEqual(sorted(serie2_labels[2][1].decode("ascii").split(",")), ['s1', 's2'])
        env.assertEqual(serie2_values, [[1, b'100'], [2, b'95']])

        actual_result = r.execute_command(
            'TS.mrange', '-', '+', 'WITHLABELS', 'FILTER', 'metric_family=cpu', 'GROUPBY', 'metric_name', 'REDUCE', 'sum')
        serie2 = actual_result[1]
        serie2_values = serie2[2]
        env.assertEqual(serie2_values, [[1, b'100'], [2, b'150']])

        actual_result = r.execute_command(
            'TS.mrange', '-', '+', 'WITHLABELS', 'FILTER', 'metric_family=cpu', 'GROUPBY', 'metric_name', 'REDUCE', 'min')
        serie2 = actual_result[1]
        serie2_values = serie2[2]
        env.assertEqual(serie2_values, [[1, b'100'], [2, b'55']])

        actual_result = r.execute_command(
            'TS.mrange', '-', '+', 'WITHLABELS', 'COUNT', 1, 'FILTER', 'metric_family=cpu', 'GROUPBY', 'metric_name', 'REDUCE', 'min')
        serie2 = actual_result[1]
        serie2_values = serie2[2]
        env.assertEqual(serie2_values, [[1, b'100']])
示例#11
0
def test_rename_none_ts():

    env = Env()
    with env.getClusterConnectionIfNeeded() as r:

        assert r.execute_command('TS.CREATE', 'a{4}')
        assert r.execute_command('SET', 'key1{4}', 'val1')
        assert r.execute_command('SET', 'key2{4}', 'val2')

        env.assertTrue(r.execute_command('RENAME', 'key1{4}', 'key3{4}'))
        env.assertTrue(r.execute_command('RENAME', 'key2{4}', 'key1{4}'))

        assert r.execute_command('SET', 'key1{4}', 'val3')
        assert r.execute_command('SET', 'key3{4}', 'val4')

        aInfo = TSInfo(r.execute_command('TS.INFO', 'a{4}'))
        env.assertEqual(aInfo.sourceKey, None)
        env.assertEqual(aInfo.rules, [])
def test_filterby():
    env = Env()
    high_temps = defaultdict(lambda: defaultdict(lambda: 0))
    specific_days = defaultdict(lambda: defaultdict(lambda: 0))
    days = [1335830400000, 1338508800000]
    for row in create_test_rdb_file.read_from_disk():
        timestamp = create_test_rdb_file.parse_timestamp(row[0])
        country = row[create_test_rdb_file.Country].replace('(', '[').replace(
            ')', ']')
        if timestamp in days:
            specific_days[country][timestamp] += 1

        if row[1] and float(row[1]) >= 30:
            if timestamp > 0:
                high_temps[country][timestamp] += 1

    with env.getClusterConnectionIfNeeded() as r:
        create_test_rdb_file.load_into_redis(r)

        def assert_results(results, expected_results):
            for row in results:
                country = row[1][0][1].decode()
                points = dict([(point[0], int(point[1])) for point in row[2]])
                for k in points:
                    env.assertEqual(points[k],
                                    expected_results[country][k],
                                    message="timestamp {} not equal".format(k))
                env.assertEqual(points,
                                expected_results[country],
                                message="country {} not eq".format(country))

        results = r.execute_command("TS.MRANGE", "-", "+", "withlabels",
                                    "FILTER_BY_VALUE", 30, 100, "AGGREGATION",
                                    "count", 3600000, "filter",
                                    "metric=temperature", "groupby", "country",
                                    "reduce", "sum")
        assert_results(results, high_temps)

        results = r.execute_command("TS.MRANGE", "-", "+", "withlabels",
                                    "FILTER_BY_TS", 1335830400000,
                                    1338508800000, "AGGREGATION", "count",
                                    3600000, "filter", "metric=temperature",
                                    "groupby", "country", "reduce", "sum")
        assert_results(results, specific_days)
示例#13
0
def test_mrange_with_expire_cmd():
    env = Env()
    set_hertz(env)

    with env.getClusterConnectionIfNeeded() as r:
        assert r.execute_command("TS.ADD", "X", "*", "1", "LABELS", "type",
                                 "DELAYED")
        assert r.execute_command("TS.ADD", "Y", "*", "1", "LABELS", "type",
                                 "DELAYED")
        assert r.execute_command("TS.ADD", "Z", "*", "1", "LABELS", "type",
                                 "DELAYED")
        current_ts = time.time()
        assert r.execute_command("EXPIRE", "X", 5)
        assert r.execute_command("EXPIRE", "Y", 6)
        assert r.execute_command("EXPIRE", "Z", 7)
        while time.time() < (current_ts + 10):
            reply = r.execute_command('TS.mrange', '-', '+', 'FILTER',
                                      'type=DELAYED')
            assert (len(reply) >= 0 and len(reply) <= 3)
        assert r.execute_command("PING")
示例#14
0
def test_non_local_data():
    env = Env()
    with env.getClusterConnectionIfNeeded() as r:
        r.execute_command('TS.ADD', '{host1}_metric_1', 1, 100, 'LABELS',
                          'metric', 'cpu')
        r.execute_command('TS.ADD', '{host1}_metric_2', 2, 40, 'LABELS',
                          'metric', 'cpu')
        r.execute_command('TS.ADD', '{host1}_metric_1', 2, 95)
        r.execute_command('TS.ADD', '{host1}_metric_1', 10, 99)

    previous_results = []
    # ensure that initiating the query on different shards always replies with the same series
    for shard in range(0, env.shardsCount):
        shard_conn = env.getConnection(shard)
        actual_result = shard_conn.execute_command(
            'TS.MRANGE - + FILTER metric=cpu')
        env.assertEqual(len(actual_result), 2)
        for previous_result in previous_results:
            ensure_replies_series_match(env, previous_result, actual_result)
        previous_results.append(actual_result)
示例#15
0
def test_rename_src():
    env = Env()
    with env.getClusterConnectionIfNeeded() as r:

        assert r.execute_command('TS.CREATE', 'a1{1}')
        assert r.execute_command('TS.CREATE', 'b{1}')

        env.assertTrue(r.execute_command('RENAME', 'a1{1}', 'a2{1}'))
        aInfo = TSInfo(r.execute_command('TS.INFO', 'a2{1}'))
        env.assertEqual(aInfo.sourceKey, None)
        env.assertEqual(aInfo.rules, [])
        
        assert r.execute_command('TS.CREATERULE', 'a2{1}', 'b{1}', 'AGGREGATION', 'AVG', 5000)
        bInfo = TSInfo(r.execute_command('TS.INFO', 'b{1}'))
        env.assertEqual(bInfo.sourceKey, b'a2{1}')
        env.assertEqual(bInfo.rules, [])

        env.assertTrue(r.execute_command('RENAME', 'a2{1}', 'a3{1}'))
        bInfo = TSInfo(r.execute_command('TS.INFO', 'b{1}'))
        env.assertEqual(bInfo.sourceKey, b'a3{1}')
        env.assertEqual(bInfo.rules, [])
示例#16
0
def test_renamenx():
    env = Env()
    with env.getClusterConnectionIfNeeded() as r:
        init(env, r)

        assert r.execute_command('RENAMENX', 't{1}', 't{1}_renamed')
        res = r.execute_command('ts.info', 't{1}_agg')
        index = res.index(b'sourceKey')
        env.assertEqual(res[index + 1], b't{1}_renamed')

        res = r.execute_command('TS.QUERYINDEX', 'name=(mush,zavi,rex)')
        env.assertEqual(sorted(res), [b't{1}_agg', b't{1}_renamed', b't{2}'])
        res = r.execute_command('TS.MGET', 'filter', 'name=(mush,zavi,rex)')
        env.assertEqual(
            sorted(res),
            [[b't{1}_agg', [], []], [b't{1}_renamed', [], [10, b'19']],
             [b't{2}', [], []]])

        assert r.execute_command('RENAMENX', 't{1}_agg', 't{1}_agg_renamed')
        res = r.execute_command('ts.info', 't{1}_renamed')
        index = res.index(b'rules')
        env.assertEqual(res[index + 1], [[b't{1}_agg_renamed', 10, b'AVG']])
def testLibmrFail():
    env = Env()
    if env.shardsCount < 3:
        env.skip()
    if (not env.isCluster):
        env.skip()
    env.skipOnSlave()  # There can't be 2 rdb save at the same time
    env.skipOnAOF()
    start_ts = 1
    samples_count = 10
    with env.getClusterConnectionIfNeeded() as r:
        assert r.execute_command('TS.CREATE', 'tester1{1}', 'LABELS', 'name',
                                 'bob')
        _insert_data(r, 'tester1{1}', start_ts, samples_count, 1)
        try:
            env.envRunner.shards[2].stopEnv()
        except Exception as e:
            pass

    Refresh_Cluster(env)
    try:
        actual_result = env.getConnection(1).execute_command(
            'TS.mrange', start_ts, start_ts + samples_count, 'WITHLABELS',
            'FILTER', 'name=bob')
        assert (False)
    except Exception as e:
        env.assertResponseError(e, "multi shard cmd failed")

    env.envRunner.shards[2].startEnv()
    Refresh_Cluster(env)
    expected_res = [[
        b'tester1{1}', [[b'name', b'bob']],
        [[1, b'1'], [2, b'1'], [3, b'1'], [4, b'1'], [5, b'1'], [6, b'1'],
         [7, b'1'], [8, b'1'], [9, b'1'], [10, b'1']]
    ]]
    actual_result = env.getConnection(1).execute_command(
        'TS.mrange', start_ts, start_ts + samples_count, 'WITHLABELS',
        'FILTER', 'name=bob')
    env.assertEqual(actual_result, expected_res)
示例#18
0
def test_restore():
    env = Env()
    with env.getClusterConnectionIfNeeded() as r:
        init(env, r)

        serialized_val = r.execute_command('DUMP', 't{1}')
        assert r.execute_command('del', 't{1}')

        res = r.execute_command('TS.QUERYINDEX', 'name=(mush,zavi,rex)')
        env.assertEqual(sorted(res), sorted({b't{2}', b't{1}_agg'}))
        res = r.execute_command('TS.MGET', 'filter', 'name=(mush,zavi,rex)')
        env.assertEqual(sorted(res),
                        sorted([[b't{1}_agg', [], []], [b't{2}', [], []]]))

        assert r.execute_command('RESTORE', 't{1}', '0', serialized_val)
        res = r.execute_command('TS.QUERYINDEX', 'name=(mush,zavi,rex)')
        env.assertEqual(sorted(res), sorted([b't{1}', b't{2}', b't{1}_agg']))
        res = r.execute_command('TS.MGET', 'filter', 'name=(mush,zavi,rex)')
        env.assertEqual(
            sorted(res),
            sorted([[b't{1}', [], [10, b'19']], [b't{2}', [], []],
                    [b't{1}_agg', [], []]]))
示例#19
0
def test_mrange_filterby():
    start_ts = 1511885909
    samples_count = 50
    env = Env()

    with env.getClusterConnectionIfNeeded() as r:
        assert r.execute_command('TS.CREATE', 'tester1', 'LABELS', 'name',
                                 'bob', 'class', 'middle', 'generation', 'x')
        assert r.execute_command('TS.CREATE', 'tester2', 'LABELS', 'name',
                                 'rudy', 'class', 'junior', 'generation', 'x')
        assert r.execute_command('TS.CREATE', 'tester3', 'LABELS', 'name',
                                 'fabi', 'class', 'top', 'generation', 'x')
        _insert_data(r, 'tester1', start_ts, samples_count, 5)
        _insert_data(r, 'tester2', start_ts, samples_count, 15)
        _insert_data(r, 'tester3', start_ts, samples_count, 25)

        with pytest.raises(redis.ResponseError) as excinfo:
            assert r.execute_command('TS.mrange', start_ts,
                                     start_ts + samples_count,
                                     'FILTER_BY_VALUE', "a", 1, 'FILTER',
                                     'name=bob')
        with pytest.raises(redis.ResponseError) as excinfo:
            assert r.execute_command('TS.mrange', start_ts,
                                     start_ts + samples_count,
                                     'FILTER_BY_VALUE', "a", "a", 'FILTER',
                                     'name=bob')
        with pytest.raises(redis.ResponseError) as excinfo:
            assert r.execute_command('TS.mrange', start_ts,
                                     start_ts + samples_count,
                                     'FILTER_BY_VALUE', 1, "a", 'FILTER',
                                     'name=bob')

        expected_result = [
            [b'tester1', [], []],
            [
                b'tester2', [],
                [[start_ts + i, str(15).encode('ascii')]
                 for i in range(samples_count)]
            ],
            [b'tester3', [], []],
        ]
        actual_result = r.execute_command('TS.mrange', start_ts,
                                          start_ts + samples_count,
                                          'FILTER_BY_VALUE', 10, 20, 'FILTER',
                                          'generation=x')
        env.assertEqual(sorted(actual_result), sorted(expected_result))

        expected_result = [
            [b'tester1', [], []],
            [
                b'tester2', [],
                [[start_ts + i, str(15).encode('ascii')] for i in range(9, 12)]
            ],
            [b'tester3', [], []],
        ]
        actual_result = r.execute_command('TS.mrange', start_ts,
                                          start_ts + samples_count,
                                          'FILTER_BY_TS', start_ts + 9,
                                          start_ts + 10, start_ts + 11,
                                          'FILTER_BY_VALUE', 10, 20, 'FILTER',
                                          'generation=x')
        env.assertEqual(sorted(actual_result), sorted(expected_result))
示例#20
0
def test_copy_compressed_uncompressed():
    env = Env()
    env.skipOnVersionSmaller("6.2.0")
    for compresssion in ["UNCOMPRESSED", 'COMPRESSED']:
        with env.getClusterConnectionIfNeeded() as r:
            r.execute_command('FLUSHALL')
            init(env, r, compression=compresssion)
            for i in range(1000):
                assert r.execute_command('TS.ADD', 't{1}', 1638304650 + i, i)

            assert r.execute_command('COPY', 't{1}', 't{1}_copied')
            t1_info = r.execute_command('ts.info', 't{1}')
            res = r.execute_command('ts.info', 't{1}_agg')
            index = res.index(b'sourceKey')
            env.assertEqual(res[index + 1], b't{1}')

            res = r.execute_command('ts.info', 't{1}_copied')
            index = res.index(b'sourceKey')
            env.assertEqual(res[index + 1], None)
            index = res.index(b'rules')
            env.assertEqual(res[index + 1], [])
            index = res.index(b'labels')
            env.assertEqual(res[index + 1], t1_info[index + 1])

            data = r.execute_command('TS.range', 't{1}', '-', '+')
            copied_data = r.execute_command(
                'TS.range',
                't{1}_copied',
                '-',
                '+',
            )
            assert data == copied_data

            res = r.execute_command('TS.QUERYINDEX', 'name=(mush,zavi,rex)')
            env.assertEqual(
                sorted(res),
                sorted([b't{1}', b't{2}', b't{1}_agg', b't{1}_copied']))
            res = r.execute_command('TS.MGET', 'filter',
                                    'name=(mush,zavi,rex)')
            env.assertEqual(
                sorted(res),
                sorted([[b't{1}', [], [1638305649, b'999']], [b't{2}', [], []],
                        [b't{1}_agg', [], [1638305630, b'984.5']],
                        [b't{1}_copied', [], [1638305649, b'999']]]))

            assert r.execute_command('COPY', 't{1}_agg', 't{1}_agg_copied')
            res = r.execute_command('ts.info', 't{1}_agg_copied')
            index = res.index(b'sourceKey')
            env.assertEqual(res[index + 1], None)
            index = res.index(b'rules')
            env.assertEqual(res[index + 1], [])

            res = r.execute_command('TS.QUERYINDEX', 'name=(mush,zavi,rex)')
            env.assertEqual(
                sorted(res),
                sorted([
                    b't{1}', b't{2}', b't{1}_agg', b't{1}_copied',
                    b't{1}_agg_copied'
                ]))
            res = r.execute_command('TS.MGET', 'filter',
                                    'name=(mush,zavi,rex)')
            env.assertEqual(
                sorted(res),
                sorted([[b't{1}', [], [1638305649, b'999']], [b't{2}', [], []],
                        [b't{1}_agg', [], [1638305630, b'984.5']],
                        [b't{1}_copied', [], [1638305649, b'999']],
                        [b't{1}_agg_copied', [], [1638305630, b'984.5']]]))
示例#21
0
class testDuplicationPolicyTests():
    def __init__(self):
        self.env = Env(moduleArgs='DUPLICATE_POLICY BLOCK')

    def test_ts_add_unknow_duplicate_policy(self):
        with self.env.getClusterConnectionIfNeeded() as r:
            with pytest.raises(redis.ResponseError) as excinfo:
                assert r.execute_command('TS.ADD', "test", 1, 1.5,
                                         "DUPLICATE_POLICY")

            with pytest.raises(redis.ResponseError) as excinfo:
                assert r.execute_command('TS.ADD', "test", 1, 1.5,
                                         "DUPLICATE_POLICY", "---------------")

    def test_precendence_key(self):
        with self.env.getClusterConnectionIfNeeded() as r:
            key = 'tester'
            key_no_dup = 'tester_no_dup'
            r.execute_command('TS.CREATE', key, 'DUPLICATE_POLICY', 'LAST')
            r.execute_command('TS.CREATE', key_no_dup)
            _fill_data(r, key)
            date_ranges = _fill_data(r, key_no_dup)

            overrided_ts = date_ranges[0][0] + 10
            overrided_value = 666
            with pytest.raises(redis.ResponseError):
                r.execute_command('TS.ADD', key_no_dup, overrided_ts,
                                  overrided_value)
            assert r.execute_command('TS.ADD', key, overrided_ts,
                                     overrided_value) == overrided_ts

            assert r.execute_command('TS.RANGE', key_no_dup, overrided_ts,
                                     overrided_ts) == [[
                                         overrided_ts,
                                         str(overrided_ts).encode('ascii')
                                     ]]
            assert r.execute_command('TS.RANGE', key, overrided_ts,
                                     overrided_ts) == [[
                                         overrided_ts,
                                         str(overrided_value).encode('ascii')
                                     ]]

            # check that inserting a non-duplicate sample doesn't fail
            non_dup_ts = date_ranges[0][1] + 1
            assert r.execute_command('TS.ADD', key_no_dup, non_dup_ts,
                                     overrided_value) == non_dup_ts

            # check that `ON_DUPLICATE` overrides the module configuration
            assert r.execute_command('TS.ADD', key_no_dup, overrided_ts,
                                     overrided_value, 'ON_DUPLICATE',
                                     'LAST') == overrided_ts
            assert r.execute_command('TS.RANGE', key_no_dup, overrided_ts,
                                     overrided_ts) == [[
                                         overrided_ts,
                                         str(overrided_value).encode('ascii')
                                     ]]

            # check that `ON_DUPLICATE` overrides the key configuration
            assert r.execute_command('TS.ADD', key, overrided_ts,
                                     overrided_value * 10, 'ON_DUPLICATE',
                                     'MAX') == overrided_ts
            assert r.execute_command('TS.RANGE', key, overrided_ts, overrided_ts) == \
                   [[overrided_ts, str(overrided_value * 10).encode('ascii')]]

    def test_policies_correctness(self):
        policies = {
            'LAST': lambda x, y: y,
            'FIRST': lambda x, y: x,
            'MIN': min,
            'MAX': max,
            'SUM': lambda x, y: x + y
        }

        with self.env.getClusterConnectionIfNeeded() as r:
            key = 'tester'

            for chunk_type in ['', 'UNCOMPRESSED']:
                r.execute_command('TS.CREATE', key, chunk_type)
                date_ranges = _fill_data(r, key)
                overrided_ts = date_ranges[0][0] + 10
                # Verified Block
                with pytest.raises(redis.ResponseError):
                    r.execute_command('TS.ADD', key, overrided_ts, 1)

                for policy in policies:
                    old_value = int(
                        r.execute_command('TS.RANGE', key, overrided_ts,
                                          overrided_ts)[0][1])
                    new_value = random.randint(-5000, 1000000)
                    assert r.execute_command('TS.ADD', key, overrided_ts,
                                             new_value, 'ON_DUPLICATE',
                                             policy) == overrided_ts
                    proccessed_value = int(
                        r.execute_command('TS.RANGE', key, overrided_ts,
                                          overrided_ts)[0][1])
                    assert policies[policy](
                        old_value, new_value
                    ) == proccessed_value, "check that {} is correct".format(
                        policy)

                r.execute_command('DEL', key)