You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by GitBox <gi...@apache.org> on 2021/10/17 00:50:55 UTC

[GitHub] [beam] damondouglas commented on a change in pull request #15721: [BEAM-13023][Playground] Implement Redis cache for pipelines' states

damondouglas commented on a change in pull request #15721:
URL: https://github.com/apache/beam/pull/15721#discussion_r729889130



##########
File path: playground/backend/internal/cache/redis_cache.go
##########
@@ -0,0 +1,145 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You 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 cache
+
+import (
+	pb "beam.apache.org/playground/backend/internal/api"
+	"context"
+	"encoding/json"
+	"fmt"
+	"github.com/go-redis/redis/v8"
+	"github.com/google/uuid"
+	"google.golang.org/grpc/grpclog"
+	"time"
+)
+
+const defaultExpirationTime = time.Minute * 15
+
+type RedisCache struct {
+	redisClient *redis.Client

Review comment:
       What if we did this pattern?  The outcome is that all the methods available to `redis.Client` are automatically bound to our struct but we can also bind our own new methods.  It was one of the rare occurences for me personally where I found value in this extends-like pattern in go.
   
   ```
   type RedisCache struct {
      *redis.Client
   }
   ```

##########
File path: playground/backend/internal/cache/redis_cache.go
##########
@@ -0,0 +1,145 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You 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 cache
+
+import (
+	pb "beam.apache.org/playground/backend/internal/api"
+	"context"
+	"encoding/json"
+	"fmt"
+	"github.com/go-redis/redis/v8"
+	"github.com/google/uuid"
+	"google.golang.org/grpc/grpclog"
+	"time"
+)
+
+const defaultExpirationTime = time.Minute * 15
+
+type RedisCache struct {
+	redisClient *redis.Client
+}
+
+// check connection to Redis
+var ping = func(redisClient *redis.Client, ctx context.Context) (string, error) {

Review comment:
       These methods were challenging for me to read.  My comment is applied to `ping` but it applies to `exists`, `expire`, etc.  May we consider either binding the methods or doing the "extend" pattern commented above?

##########
File path: playground/backend/internal/cache/redis_cache.go
##########
@@ -0,0 +1,145 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You 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 cache
+
+import (
+	pb "beam.apache.org/playground/backend/internal/api"
+	"context"
+	"encoding/json"
+	"fmt"
+	"github.com/go-redis/redis/v8"
+	"github.com/google/uuid"
+	"google.golang.org/grpc/grpclog"
+	"time"
+)
+
+const defaultExpirationTime = time.Minute * 15
+
+type RedisCache struct {
+	redisClient *redis.Client
+}
+
+// check connection to Redis
+var ping = func(redisClient *redis.Client, ctx context.Context) (string, error) {
+	return redisClient.Ping(ctx).Result()
+}
+
+// check if pipelineId exists as Redis key
+var exists = func(redisClient *redis.Client, ctx context.Context, pipelineId uuid.UUID) (int64, error) {
+	return redisClient.Exists(ctx, pipelineId.String()).Result()
+}
+
+// set expiration time for key
+var expire = func(redisClient *redis.Client, ctx context.Context, key string, expTime time.Duration) (bool, error) {
+	return redisClient.Expire(ctx, key, expTime).Result()
+}
+
+// put encoded value to {key}:{encoded subKey} structure
+var hSet = func(redisClient *redis.Client, ctx context.Context, key string, subKeyMarsh, valueMarsh []byte) (int64, error) {
+	return redisClient.HSet(ctx, key, subKeyMarsh, valueMarsh).Result()
+}
+
+// receive value from {key}:{subKey} structure
+var hGet = func(redisClient *redis.Client, ctx context.Context, key, subKey string) (string, error) {
+	return redisClient.HGet(ctx, key, subKey).Result()
+}
+
+// newRedisCache returns Redis implementation of Cache interface.
+// In case of problem with connection to Redis returns error.
+func newRedisCache(ctx context.Context, addr string) (*RedisCache, error) {

Review comment:
       How would a user of this package instantiate `RedisCache` if this is not public?  Also this constructor seems more restrictive than the original redis client.

##########
File path: playground/backend/internal/cache/redis_cache.go
##########
@@ -0,0 +1,145 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You 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 cache
+
+import (
+	pb "beam.apache.org/playground/backend/internal/api"
+	"context"
+	"encoding/json"
+	"fmt"
+	"github.com/go-redis/redis/v8"
+	"github.com/google/uuid"
+	"google.golang.org/grpc/grpclog"
+	"time"
+)
+
+const defaultExpirationTime = time.Minute * 15
+
+type RedisCache struct {
+	redisClient *redis.Client
+}
+
+// check connection to Redis
+var ping = func(redisClient *redis.Client, ctx context.Context) (string, error) {
+	return redisClient.Ping(ctx).Result()
+}
+
+// check if pipelineId exists as Redis key
+var exists = func(redisClient *redis.Client, ctx context.Context, pipelineId uuid.UUID) (int64, error) {
+	return redisClient.Exists(ctx, pipelineId.String()).Result()
+}
+
+// set expiration time for key
+var expire = func(redisClient *redis.Client, ctx context.Context, key string, expTime time.Duration) (bool, error) {
+	return redisClient.Expire(ctx, key, expTime).Result()
+}
+
+// put encoded value to {key}:{encoded subKey} structure
+var hSet = func(redisClient *redis.Client, ctx context.Context, key string, subKeyMarsh, valueMarsh []byte) (int64, error) {
+	return redisClient.HSet(ctx, key, subKeyMarsh, valueMarsh).Result()
+}
+
+// receive value from {key}:{subKey} structure
+var hGet = func(redisClient *redis.Client, ctx context.Context, key, subKey string) (string, error) {
+	return redisClient.HGet(ctx, key, subKey).Result()
+}
+
+// newRedisCache returns Redis implementation of Cache interface.
+// In case of problem with connection to Redis returns error.
+func newRedisCache(ctx context.Context, addr string) (*RedisCache, error) {
+	rc := RedisCache{redisClient: redis.NewClient(&redis.Options{Addr: addr})}
+	_, err := ping(rc.redisClient, ctx)
+	if err != nil {
+		grpclog.Errorf("Redis Cache: connect to Redis: error during Ping operation, err: %s", err.Error())
+		return nil, err
+	}
+	return &rc, nil
+}
+
+func (rc *RedisCache) GetValue(ctx context.Context, pipelineId uuid.UUID, subKey SubKey) (interface{}, error) {
+	marshSubKey, err := json.Marshal(subKey)
+	if err != nil {
+		grpclog.Errorf("Redis Cache: get value: error during marshal subKey: %s, err: %s", subKey, err.Error())
+		return nil, err
+	}
+	value, err := hGet(rc.redisClient, ctx, pipelineId.String(), string(marshSubKey))
+	if err != nil {
+		grpclog.Errorf("Redis Cache: get value: error during HGet operation for key: %s, subKey: %s, err: %s", pipelineId.String(), subKey, err.Error())
+		return nil, err
+	}
+
+	return unmarshalBySubKey(subKey, value)
+}
+
+func (rc *RedisCache) SetValue(ctx context.Context, pipelineId uuid.UUID, subKey SubKey, value interface{}) error {
+	subKeyMarsh, err := json.Marshal(subKey)
+	if err != nil {
+		grpclog.Errorf("Redis Cache: set value: error during marshal subKey: %s, err: %s", subKey, err.Error())

Review comment:
       May we consider just using the `log` library native to go?

##########
File path: playground/backend/internal/cache/redis_cache.go
##########
@@ -0,0 +1,145 @@
+// Licensed to the Apache Software Foundation (ASF) under one or more
+// contributor license agreements.  See the NOTICE file distributed with
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You 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 cache
+
+import (
+	pb "beam.apache.org/playground/backend/internal/api"
+	"context"
+	"encoding/json"
+	"fmt"
+	"github.com/go-redis/redis/v8"
+	"github.com/google/uuid"
+	"google.golang.org/grpc/grpclog"
+	"time"
+)
+
+const defaultExpirationTime = time.Minute * 15
+
+type RedisCache struct {
+	redisClient *redis.Client
+}
+
+// check connection to Redis
+var ping = func(redisClient *redis.Client, ctx context.Context) (string, error) {
+	return redisClient.Ping(ctx).Result()
+}
+
+// check if pipelineId exists as Redis key
+var exists = func(redisClient *redis.Client, ctx context.Context, pipelineId uuid.UUID) (int64, error) {
+	return redisClient.Exists(ctx, pipelineId.String()).Result()
+}
+
+// set expiration time for key
+var expire = func(redisClient *redis.Client, ctx context.Context, key string, expTime time.Duration) (bool, error) {
+	return redisClient.Expire(ctx, key, expTime).Result()
+}
+
+// put encoded value to {key}:{encoded subKey} structure
+var hSet = func(redisClient *redis.Client, ctx context.Context, key string, subKeyMarsh, valueMarsh []byte) (int64, error) {
+	return redisClient.HSet(ctx, key, subKeyMarsh, valueMarsh).Result()
+}
+
+// receive value from {key}:{subKey} structure
+var hGet = func(redisClient *redis.Client, ctx context.Context, key, subKey string) (string, error) {
+	return redisClient.HGet(ctx, key, subKey).Result()
+}
+
+// newRedisCache returns Redis implementation of Cache interface.
+// In case of problem with connection to Redis returns error.
+func newRedisCache(ctx context.Context, addr string) (*RedisCache, error) {

Review comment:
       I now see with https://github.com/apache/beam/pull/15714/files#diff-c9958b6146b1d7a3cc749646a8807db23e6d2d35bd44ab0b3fb775d6b445e64fR48 how this is intended to be used.  Perhaps then it makes sense to not make `RedisCache` public.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org