You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by "damondouglas (via GitHub)" <gi...@apache.org> on 2023/10/20 00:19:05 UTC

[PR] [RRIO] create RedisClient utility [beam]

damondouglas opened a new pull request, #29081:
URL: https://github.com/apache/beam/pull/29081

   This PR addresses #28932, addresses #28924, addresses #28922 by creating a RedisClient utility. The value of this class over using the direct java Redis client is that it is Serializable and handles the setup/teardown with null checks needed when using a non-Serializeable client.
   
   ------------------------
   
   Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:
   
    - [x] Mention the appropriate issue in your description (for example: `addresses #123`), if applicable. This will automatically add a link to the pull request in the issue. If you would like the issue to automatically close on merging the pull request, comment `fixes #<ISSUE NUMBER>` instead.
    - [ ] Update `CHANGES.md` with noteworthy changes.
    - [ ] If this contribution is large, please file an Apache [Individual Contributor License Agreement](https://www.apache.org/licenses/icla.pdf).
   
   See the [Contributor Guide](https://beam.apache.org/contribute) for more tips on [how to make review process smoother](https://github.com/apache/beam/blob/master/CONTRIBUTING.md#make-the-reviewers-job-easier).
   
   To check the build health, please visit [https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md](https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md)
   
   GitHub Actions Tests Status (on master branch)
   ------------------------------------------------------------------------------------------------
   [![Build python source distribution and wheels](https://github.com/apache/beam/workflows/Build%20python%20source%20distribution%20and%20wheels/badge.svg?branch=master&event=schedule)](https://github.com/apache/beam/actions?query=workflow%3A%22Build+python+source+distribution+and+wheels%22+branch%3Amaster+event%3Aschedule)
   [![Python tests](https://github.com/apache/beam/workflows/Python%20tests/badge.svg?branch=master&event=schedule)](https://github.com/apache/beam/actions?query=workflow%3A%22Python+Tests%22+branch%3Amaster+event%3Aschedule)
   [![Java tests](https://github.com/apache/beam/workflows/Java%20Tests/badge.svg?branch=master&event=schedule)](https://github.com/apache/beam/actions?query=workflow%3A%22Java+Tests%22+branch%3Amaster+event%3Aschedule)
   [![Go tests](https://github.com/apache/beam/workflows/Go%20tests/badge.svg?branch=master&event=schedule)](https://github.com/apache/beam/actions?query=workflow%3A%22Go+tests%22+branch%3Amaster+event%3Aschedule)
   
   See [CI.md](https://github.com/apache/beam/blob/master/CI.md) for more information about GitHub Actions CI or the [workflows README](https://github.com/apache/beam/blob/master/.github/workflows/README.md) to see a list of phrases to trigger workflows.
   


-- 
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


Re: [PR] [RRIO] create RedisClient utility [beam]

Posted by "bvolpato (via GitHub)" <gi...@apache.org>.
bvolpato commented on code in PR #29081:
URL: https://github.com/apache/beam/pull/29081#discussion_r1367245483


##########
sdks/java/io/rrio/src/main/java/org/apache/beam/io/requestresponse/RedisClient.java:
##########
@@ -0,0 +1,183 @@
+/*
+ * 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 org.apache.beam.io.requestresponse;
+
+import static org.apache.beam.sdk.util.Preconditions.checkStateNotNull;
+
+import java.net.URI;
+import java.nio.charset.StandardCharsets;
+import org.apache.beam.sdk.transforms.DoFn;
+import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
+import org.checkerframework.checker.nullness.qual.NonNull;
+import org.joda.time.Duration;
+import redis.clients.jedis.JedisPooled;
+import redis.clients.jedis.exceptions.JedisException;
+
+/**
+ * {@link RedisClient} is a convenience class that supports operations needed for caching by various
+ * transforms in this package. It implements the {@link SetupTeardown} interface for ease-of-use
+ * within a {@link DoFn} context. Unlike the underlying {@link JedisPooled} client, {@link
+ * RedisClient} is {@link java.io.Serializable}.
+ */
+class RedisClient implements SetupTeardown {
+
+  private final URI uri;
+
+  private transient @MonotonicNonNull JedisPooled jedis;
+
+  /**
+   * Instantiates a {@link RedisClient}. {@link URI} expected of the form: {@code
+   * redis://<host>:<port>}.
+   */
+  RedisClient(URI uri) {
+    this.uri = uri;
+  }
+
+  /**
+   * Decrement a value stored by the key, returning the resulting decremented value. Per Redis
+   * convention, sets the value to -1 for keys that do not exist. Naming of this method preserves
+   * that of the underlying {@link JedisPooled} client and performs a null check prior to execution.
+   */
+  long decr(String key) throws UserCodeExecutionException {
+    try {
+      return getSafeClient().decr(key);
+    } catch (JedisException e) {
+      throw new UserCodeExecutionException(e);
+    }
+  }
+
+  /**
+   * Get the long value stored by the key. Yields zero when key does not exist, keeping consistency
+   * with Redis convention. Consider using {@link #exists} to query key existance.
+   */
+  long getLong(String key) throws UserCodeExecutionException {
+    try {
+      return getSafeClient().decrBy(key, 0L);
+    } catch (JedisException e) {
+      throw new UserCodeExecutionException(e);
+    }
+  }
+
+  /** Query whether the key exists. */
+  boolean exists(String key) throws UserCodeExecutionException {
+    try {
+      return getSafeClient().exists(key);
+    } catch (JedisException e) {
+      throw new UserCodeExecutionException(e);
+    }
+  }
+
+  /**
+   * Increment a value stored by the key, returning the resulting decremented value. Sets the value
+   * to 1, key does not exist, per Redis convention. Naming of this method preserves that of the
+   * underlying {@link JedisPooled} client and performs a null check prior to execution.
+   */
+  long incr(String key) throws UserCodeExecutionException {
+    try {
+      return getSafeClient().incr(key);
+    } catch (JedisException e) {
+      throw new UserCodeExecutionException(e);
+    }
+  }
+
+  /**
+   * Query the size of a list identified by the key. Returns 0 if key does not exist, per Redis
+   * convention. Naming of this method preserves that of the underlying {@link JedisPooled} client
+   * and performs a null check prior to execution.
+   */
+  long llen(String key) throws UserCodeExecutionException {
+    try {
+      return getSafeClient().llen(key);
+    } catch (JedisException e) {
+      throw new UserCodeExecutionException(e);
+    }
+  }
+
+  /**
+   * Pushes items to the back ('right') of the list. Naming of this method preserves that of the
+   * underlying {@link JedisPooled} client and performs a null check prior to execution.
+   */
+  void rpush(String key, byte[]... items) throws UserCodeExecutionException {
+    try {
+      getSafeClient().lpush(key.getBytes(StandardCharsets.UTF_8), items);

Review Comment:
   rpush or lpush? the delegated method disagrees, if this is right can you write a short comment explaining?



-- 
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


Re: [PR] [RRIO] create RedisClient utility [beam]

Posted by "damondouglas (via GitHub)" <gi...@apache.org>.
damondouglas commented on code in PR #29081:
URL: https://github.com/apache/beam/pull/29081#discussion_r1369240151


##########
sdks/java/io/rrio/src/main/java/org/apache/beam/io/requestresponse/RedisClient.java:
##########
@@ -0,0 +1,183 @@
+/*
+ * 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 org.apache.beam.io.requestresponse;
+
+import static org.apache.beam.sdk.util.Preconditions.checkStateNotNull;
+
+import java.net.URI;
+import java.nio.charset.StandardCharsets;
+import org.apache.beam.sdk.transforms.DoFn;
+import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
+import org.checkerframework.checker.nullness.qual.NonNull;
+import org.joda.time.Duration;
+import redis.clients.jedis.JedisPooled;
+import redis.clients.jedis.exceptions.JedisException;
+
+/**
+ * {@link RedisClient} is a convenience class that supports operations needed for caching by various
+ * transforms in this package. It implements the {@link SetupTeardown} interface for ease-of-use
+ * within a {@link DoFn} context. Unlike the underlying {@link JedisPooled} client, {@link
+ * RedisClient} is {@link java.io.Serializable}.
+ */
+class RedisClient implements SetupTeardown {

Review Comment:
   @johnjcasey I will go ahead and resolve this and thank you again for reviewing. Please re-open if you feel strongly about something I may have missed regarding exposing these methods via the public modifier.



-- 
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


Re: [PR] [RRIO] create RedisClient utility [beam]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #29081:
URL: https://github.com/apache/beam/pull/29081#issuecomment-1773046399

   Assigning reviewers. If you would like to opt out of this review, comment `assign to next reviewer`:
   
   R: @bvolpato for label java.
   R: @damccorm for label build.
   R: @johnjcasey for label io.
   
   Available commands:
   - `stop reviewer notifications` - opt out of the automated review tooling
   - `remind me after tests pass` - tag the comment author after tests pass
   - `waiting on author` - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)
   
   The PR bot will only process comments in the main thread (not review comments).


-- 
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


Re: [PR] [RRIO] create RedisClient utility [beam]

Posted by "damondouglas (via GitHub)" <gi...@apache.org>.
damondouglas commented on code in PR #29081:
URL: https://github.com/apache/beam/pull/29081#discussion_r1367221489


##########
.github/workflows/beam_PreCommit_Java_RequestResponse_IO_Direct.yml:
##########
@@ -85,7 +85,7 @@ jobs:
     - name: run RequestResponse IO build script
       uses: ./.github/actions/gradle-command-self-hosted-action
       with:
-        gradle-command: :sdks:java:io:RequestResponse:build
+        gradle-command: :sdks:java:io:rrio:build

Review Comment:
   FYI This check is failing because of the incorrect gradle project path. I fixed it but this PR will not fully pass until it is merged.
   https://github.com/apache/beam/actions/runs/6590268861/job/17906530663?pr=29081#step:6:51



-- 
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


Re: [PR] [RRIO] create RedisClient utility [beam]

Posted by "bvolpato (via GitHub)" <gi...@apache.org>.
bvolpato commented on code in PR #29081:
URL: https://github.com/apache/beam/pull/29081#discussion_r1367246020


##########
sdks/java/io/rrio/src/main/java/org/apache/beam/io/requestresponse/RedisClient.java:
##########
@@ -0,0 +1,183 @@
+/*
+ * 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 org.apache.beam.io.requestresponse;
+
+import static org.apache.beam.sdk.util.Preconditions.checkStateNotNull;
+
+import java.net.URI;
+import java.nio.charset.StandardCharsets;
+import org.apache.beam.sdk.transforms.DoFn;
+import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
+import org.checkerframework.checker.nullness.qual.NonNull;
+import org.joda.time.Duration;
+import redis.clients.jedis.JedisPooled;
+import redis.clients.jedis.exceptions.JedisException;
+
+/**
+ * {@link RedisClient} is a convenience class that supports operations needed for caching by various
+ * transforms in this package. It implements the {@link SetupTeardown} interface for ease-of-use
+ * within a {@link DoFn} context. Unlike the underlying {@link JedisPooled} client, {@link
+ * RedisClient} is {@link java.io.Serializable}.
+ */
+class RedisClient implements SetupTeardown {
+
+  private final URI uri;
+
+  private transient @MonotonicNonNull JedisPooled jedis;
+
+  /**
+   * Instantiates a {@link RedisClient}. {@link URI} expected of the form: {@code
+   * redis://<host>:<port>}.
+   */
+  RedisClient(URI uri) {
+    this.uri = uri;
+  }
+
+  /**
+   * Decrement a value stored by the key, returning the resulting decremented value. Per Redis
+   * convention, sets the value to -1 for keys that do not exist. Naming of this method preserves
+   * that of the underlying {@link JedisPooled} client and performs a null check prior to execution.
+   */
+  long decr(String key) throws UserCodeExecutionException {
+    try {
+      return getSafeClient().decr(key);
+    } catch (JedisException e) {
+      throw new UserCodeExecutionException(e);
+    }
+  }
+
+  /**
+   * Get the long value stored by the key. Yields zero when key does not exist, keeping consistency
+   * with Redis convention. Consider using {@link #exists} to query key existance.
+   */
+  long getLong(String key) throws UserCodeExecutionException {
+    try {
+      return getSafeClient().decrBy(key, 0L);
+    } catch (JedisException e) {
+      throw new UserCodeExecutionException(e);
+    }
+  }
+
+  /** Query whether the key exists. */
+  boolean exists(String key) throws UserCodeExecutionException {
+    try {
+      return getSafeClient().exists(key);
+    } catch (JedisException e) {
+      throw new UserCodeExecutionException(e);
+    }
+  }
+
+  /**
+   * Increment a value stored by the key, returning the resulting decremented value. Sets the value
+   * to 1, key does not exist, per Redis convention. Naming of this method preserves that of the
+   * underlying {@link JedisPooled} client and performs a null check prior to execution.
+   */
+  long incr(String key) throws UserCodeExecutionException {
+    try {
+      return getSafeClient().incr(key);
+    } catch (JedisException e) {
+      throw new UserCodeExecutionException(e);
+    }
+  }
+
+  /**
+   * Query the size of a list identified by the key. Returns 0 if key does not exist, per Redis
+   * convention. Naming of this method preserves that of the underlying {@link JedisPooled} client
+   * and performs a null check prior to execution.
+   */
+  long llen(String key) throws UserCodeExecutionException {
+    try {
+      return getSafeClient().llen(key);
+    } catch (JedisException e) {
+      throw new UserCodeExecutionException(e);
+    }
+  }
+
+  /**
+   * Pushes items to the back ('right') of the list. Naming of this method preserves that of the
+   * underlying {@link JedisPooled} client and performs a null check prior to execution.
+   */
+  void rpush(String key, byte[]... items) throws UserCodeExecutionException {
+    try {
+      getSafeClient().lpush(key.getBytes(StandardCharsets.UTF_8), items);
+    } catch (JedisException e) {
+      throw new UserCodeExecutionException(e);
+    }
+  }
+
+  /**
+   * Pops items from the front ('left') of the list. Naming of this method preserves that of the
+   * underlying {@link JedisPooled} client and performs a null check prior to execution.
+   */
+  byte[] lpop(String key) throws UserCodeExecutionException {
+    try {
+      return getSafeClient().rpop(key.getBytes(StandardCharsets.UTF_8));

Review Comment:
   same



-- 
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


Re: [PR] [RRIO] create RedisClient utility [beam]

Posted by "johnjcasey (via GitHub)" <gi...@apache.org>.
johnjcasey commented on code in PR #29081:
URL: https://github.com/apache/beam/pull/29081#discussion_r1367284242


##########
sdks/java/io/rrio/src/main/java/org/apache/beam/io/requestresponse/RedisClient.java:
##########
@@ -0,0 +1,183 @@
+/*
+ * 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 org.apache.beam.io.requestresponse;
+
+import static org.apache.beam.sdk.util.Preconditions.checkStateNotNull;
+
+import java.net.URI;
+import java.nio.charset.StandardCharsets;
+import org.apache.beam.sdk.transforms.DoFn;
+import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
+import org.checkerframework.checker.nullness.qual.NonNull;
+import org.joda.time.Duration;
+import redis.clients.jedis.JedisPooled;
+import redis.clients.jedis.exceptions.JedisException;
+
+/**
+ * {@link RedisClient} is a convenience class that supports operations needed for caching by various
+ * transforms in this package. It implements the {@link SetupTeardown} interface for ease-of-use
+ * within a {@link DoFn} context. Unlike the underlying {@link JedisPooled} client, {@link
+ * RedisClient} is {@link java.io.Serializable}.
+ */
+class RedisClient implements SetupTeardown {

Review Comment:
   Its a little strange that none of these methods are public or private



-- 
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


Re: [PR] [RRIO] create RedisClient utility [beam]

Posted by "damondouglas (via GitHub)" <gi...@apache.org>.
damondouglas commented on code in PR #29081:
URL: https://github.com/apache/beam/pull/29081#discussion_r1367399842


##########
sdks/java/io/rrio/src/main/java/org/apache/beam/io/requestresponse/RedisClient.java:
##########
@@ -0,0 +1,183 @@
+/*
+ * 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 org.apache.beam.io.requestresponse;
+
+import static org.apache.beam.sdk.util.Preconditions.checkStateNotNull;
+
+import java.net.URI;
+import java.nio.charset.StandardCharsets;
+import org.apache.beam.sdk.transforms.DoFn;
+import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
+import org.checkerframework.checker.nullness.qual.NonNull;
+import org.joda.time.Duration;
+import redis.clients.jedis.JedisPooled;
+import redis.clients.jedis.exceptions.JedisException;
+
+/**
+ * {@link RedisClient} is a convenience class that supports operations needed for caching by various
+ * transforms in this package. It implements the {@link SetupTeardown} interface for ease-of-use
+ * within a {@link DoFn} context. Unlike the underlying {@link JedisPooled} client, {@link
+ * RedisClient} is {@link java.io.Serializable}.
+ */
+class RedisClient implements SetupTeardown {

Review Comment:
   They are needed as package private. Private would prevent their use by other classes in the `org.apache.beam.io.requestresponse` package. Public would expose it outside `org.apache.beam.io.requestresponse` and there isn't _yet_ a reason to do this. This class will be used by `CacheRead` and `CacheWrite`, for example, classes that are package private. Could you tell me why you think there might be value in making this 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


Re: [PR] [RRIO] create RedisClient utility [beam]

Posted by "damondouglas (via GitHub)" <gi...@apache.org>.
damondouglas merged PR #29081:
URL: https://github.com/apache/beam/pull/29081


-- 
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


Re: [PR] [RRIO] create RedisClient utility [beam]

Posted by "damondouglas (via GitHub)" <gi...@apache.org>.
damondouglas commented on code in PR #29081:
URL: https://github.com/apache/beam/pull/29081#discussion_r1367257675


##########
sdks/java/io/rrio/src/main/java/org/apache/beam/io/requestresponse/RedisClient.java:
##########
@@ -0,0 +1,183 @@
+/*
+ * 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 org.apache.beam.io.requestresponse;
+
+import static org.apache.beam.sdk.util.Preconditions.checkStateNotNull;
+
+import java.net.URI;
+import java.nio.charset.StandardCharsets;
+import org.apache.beam.sdk.transforms.DoFn;
+import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
+import org.checkerframework.checker.nullness.qual.NonNull;
+import org.joda.time.Duration;
+import redis.clients.jedis.JedisPooled;
+import redis.clients.jedis.exceptions.JedisException;
+
+/**
+ * {@link RedisClient} is a convenience class that supports operations needed for caching by various
+ * transforms in this package. It implements the {@link SetupTeardown} interface for ease-of-use
+ * within a {@link DoFn} context. Unlike the underlying {@link JedisPooled} client, {@link
+ * RedisClient} is {@link java.io.Serializable}.
+ */
+class RedisClient implements SetupTeardown {
+
+  private final URI uri;
+
+  private transient @MonotonicNonNull JedisPooled jedis;
+
+  /**
+   * Instantiates a {@link RedisClient}. {@link URI} expected of the form: {@code
+   * redis://<host>:<port>}.
+   */
+  RedisClient(URI uri) {
+    this.uri = uri;
+  }
+
+  /**
+   * Decrement a value stored by the key, returning the resulting decremented value. Per Redis
+   * convention, sets the value to -1 for keys that do not exist. Naming of this method preserves
+   * that of the underlying {@link JedisPooled} client and performs a null check prior to execution.
+   */
+  long decr(String key) throws UserCodeExecutionException {
+    try {
+      return getSafeClient().decr(key);
+    } catch (JedisException e) {
+      throw new UserCodeExecutionException(e);
+    }
+  }
+
+  /**
+   * Get the long value stored by the key. Yields zero when key does not exist, keeping consistency
+   * with Redis convention. Consider using {@link #exists} to query key existance.
+   */
+  long getLong(String key) throws UserCodeExecutionException {
+    try {
+      return getSafeClient().decrBy(key, 0L);
+    } catch (JedisException e) {
+      throw new UserCodeExecutionException(e);
+    }
+  }
+
+  /** Query whether the key exists. */
+  boolean exists(String key) throws UserCodeExecutionException {
+    try {
+      return getSafeClient().exists(key);
+    } catch (JedisException e) {
+      throw new UserCodeExecutionException(e);
+    }
+  }
+
+  /**
+   * Increment a value stored by the key, returning the resulting decremented value. Sets the value
+   * to 1, key does not exist, per Redis convention. Naming of this method preserves that of the
+   * underlying {@link JedisPooled} client and performs a null check prior to execution.
+   */
+  long incr(String key) throws UserCodeExecutionException {
+    try {
+      return getSafeClient().incr(key);
+    } catch (JedisException e) {
+      throw new UserCodeExecutionException(e);
+    }
+  }
+
+  /**
+   * Query the size of a list identified by the key. Returns 0 if key does not exist, per Redis
+   * convention. Naming of this method preserves that of the underlying {@link JedisPooled} client
+   * and performs a null check prior to execution.
+   */
+  long llen(String key) throws UserCodeExecutionException {
+    try {
+      return getSafeClient().llen(key);
+    } catch (JedisException e) {
+      throw new UserCodeExecutionException(e);
+    }
+  }
+
+  /**
+   * Pushes items to the back ('right') of the list. Naming of this method preserves that of the
+   * underlying {@link JedisPooled} client and performs a null check prior to execution.
+   */
+  void rpush(String key, byte[]... items) throws UserCodeExecutionException {
+    try {
+      getSafeClient().lpush(key.getBytes(StandardCharsets.UTF_8), items);
+    } catch (JedisException e) {
+      throw new UserCodeExecutionException(e);
+    }
+  }
+
+  /**
+   * Pops items from the front ('left') of the list. Naming of this method preserves that of the
+   * underlying {@link JedisPooled} client and performs a null check prior to execution.
+   */
+  byte[] lpop(String key) throws UserCodeExecutionException {
+    try {
+      return getSafeClient().rpop(key.getBytes(StandardCharsets.UTF_8));

Review Comment:
   Thank you!



-- 
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


Re: [PR] [RRIO] create RedisClient utility [beam]

Posted by "damondouglas (via GitHub)" <gi...@apache.org>.
damondouglas commented on PR #29081:
URL: https://github.com/apache/beam/pull/29081#issuecomment-1773044405

   assign set of reviewers


-- 
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


Re: [PR] [RRIO] create RedisClient utility [beam]

Posted by "damondouglas (via GitHub)" <gi...@apache.org>.
damondouglas commented on code in PR #29081:
URL: https://github.com/apache/beam/pull/29081#discussion_r1367252885


##########
sdks/java/io/rrio/src/main/java/org/apache/beam/io/requestresponse/RedisClient.java:
##########
@@ -0,0 +1,183 @@
+/*
+ * 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 org.apache.beam.io.requestresponse;
+
+import static org.apache.beam.sdk.util.Preconditions.checkStateNotNull;
+
+import java.net.URI;
+import java.nio.charset.StandardCharsets;
+import org.apache.beam.sdk.transforms.DoFn;
+import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
+import org.checkerframework.checker.nullness.qual.NonNull;
+import org.joda.time.Duration;
+import redis.clients.jedis.JedisPooled;
+import redis.clients.jedis.exceptions.JedisException;
+
+/**
+ * {@link RedisClient} is a convenience class that supports operations needed for caching by various
+ * transforms in this package. It implements the {@link SetupTeardown} interface for ease-of-use
+ * within a {@link DoFn} context. Unlike the underlying {@link JedisPooled} client, {@link
+ * RedisClient} is {@link java.io.Serializable}.
+ */
+class RedisClient implements SetupTeardown {
+
+  private final URI uri;
+
+  private transient @MonotonicNonNull JedisPooled jedis;
+
+  /**
+   * Instantiates a {@link RedisClient}. {@link URI} expected of the form: {@code
+   * redis://<host>:<port>}.
+   */
+  RedisClient(URI uri) {
+    this.uri = uri;
+  }
+
+  /**
+   * Decrement a value stored by the key, returning the resulting decremented value. Per Redis
+   * convention, sets the value to -1 for keys that do not exist. Naming of this method preserves
+   * that of the underlying {@link JedisPooled} client and performs a null check prior to execution.
+   */
+  long decr(String key) throws UserCodeExecutionException {
+    try {
+      return getSafeClient().decr(key);
+    } catch (JedisException e) {
+      throw new UserCodeExecutionException(e);
+    }
+  }
+
+  /**
+   * Get the long value stored by the key. Yields zero when key does not exist, keeping consistency
+   * with Redis convention. Consider using {@link #exists} to query key existance.
+   */
+  long getLong(String key) throws UserCodeExecutionException {
+    try {
+      return getSafeClient().decrBy(key, 0L);
+    } catch (JedisException e) {
+      throw new UserCodeExecutionException(e);
+    }
+  }
+
+  /** Query whether the key exists. */
+  boolean exists(String key) throws UserCodeExecutionException {
+    try {
+      return getSafeClient().exists(key);
+    } catch (JedisException e) {
+      throw new UserCodeExecutionException(e);
+    }
+  }
+
+  /**
+   * Increment a value stored by the key, returning the resulting decremented value. Sets the value
+   * to 1, key does not exist, per Redis convention. Naming of this method preserves that of the
+   * underlying {@link JedisPooled} client and performs a null check prior to execution.
+   */
+  long incr(String key) throws UserCodeExecutionException {
+    try {
+      return getSafeClient().incr(key);
+    } catch (JedisException e) {
+      throw new UserCodeExecutionException(e);
+    }
+  }
+
+  /**
+   * Query the size of a list identified by the key. Returns 0 if key does not exist, per Redis
+   * convention. Naming of this method preserves that of the underlying {@link JedisPooled} client
+   * and performs a null check prior to execution.
+   */
+  long llen(String key) throws UserCodeExecutionException {
+    try {
+      return getSafeClient().llen(key);
+    } catch (JedisException e) {
+      throw new UserCodeExecutionException(e);
+    }
+  }
+
+  /**
+   * Pushes items to the back ('right') of the list. Naming of this method preserves that of the
+   * underlying {@link JedisPooled} client and performs a null check prior to execution.
+   */
+  void rpush(String key, byte[]... items) throws UserCodeExecutionException {
+    try {
+      getSafeClient().lpush(key.getBytes(StandardCharsets.UTF_8), items);

Review Comment:
   Oh good catch!



-- 
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


Re: [PR] [RRIO] create RedisClient utility [beam]

Posted by "damondouglas (via GitHub)" <gi...@apache.org>.
damondouglas commented on code in PR #29081:
URL: https://github.com/apache/beam/pull/29081#discussion_r1367399842


##########
sdks/java/io/rrio/src/main/java/org/apache/beam/io/requestresponse/RedisClient.java:
##########
@@ -0,0 +1,183 @@
+/*
+ * 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 org.apache.beam.io.requestresponse;
+
+import static org.apache.beam.sdk.util.Preconditions.checkStateNotNull;
+
+import java.net.URI;
+import java.nio.charset.StandardCharsets;
+import org.apache.beam.sdk.transforms.DoFn;
+import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
+import org.checkerframework.checker.nullness.qual.NonNull;
+import org.joda.time.Duration;
+import redis.clients.jedis.JedisPooled;
+import redis.clients.jedis.exceptions.JedisException;
+
+/**
+ * {@link RedisClient} is a convenience class that supports operations needed for caching by various
+ * transforms in this package. It implements the {@link SetupTeardown} interface for ease-of-use
+ * within a {@link DoFn} context. Unlike the underlying {@link JedisPooled} client, {@link
+ * RedisClient} is {@link java.io.Serializable}.
+ */
+class RedisClient implements SetupTeardown {

Review Comment:
   They are needed as package private. Private would prevent their use by other classes in the `org.apache.beam.io.requestresponse` package. Public would expose it outside `org.apache.beam.io.requestresponse` and there isn't _yet_ a reason to do this. This class will be used by `CacheRead` and `CacheWrite`, for example, classes that are package private. Could you tell me why you think there might be value in making this public? Tests would not have access to these methods if they were private.



-- 
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