tkalkirill commented on code in PR #2950:
URL: https://github.com/apache/ignite-3/pull/2950#discussion_r1425009788


##########
modules/raft/src/main/java/org/apache/ignite/internal/raft/util/DefaultByteBuffersPool.java:
##########
@@ -0,0 +1,72 @@
+/*
+ * 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.ignite.internal.raft.util;
+
+import java.nio.ByteBuffer;
+import java.util.Queue;
+import java.util.concurrent.ConcurrentLinkedQueue;
+import java.util.concurrent.atomic.AtomicInteger;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Basic pool implementation with limited capacity.
+ */

Review Comment:
   ```suggestion
   /** Basic pool implementation with limited capacity. */
   ```



##########
modules/raft/src/main/java/org/apache/ignite/internal/raft/util/DefaultByteBuffersPool.java:
##########
@@ -0,0 +1,72 @@
+/*
+ * 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.ignite.internal.raft.util;
+
+import java.nio.ByteBuffer;
+import java.util.Queue;
+import java.util.concurrent.ConcurrentLinkedQueue;
+import java.util.concurrent.atomic.AtomicInteger;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Basic pool implementation with limited capacity.
+ */
+public class DefaultByteBuffersPool implements ByteBuffersPool {
+    /** Max pool size. */
+    private final int capacity;
+
+    /** Queue with cached buffers. */
+    private final Queue<ByteBuffer> queue;
+
+    /** Pool size. */
+    private final AtomicInteger size = new AtomicInteger();
+
+    /**
+     * Constructor.
+     *
+     * @param capacity Max pool size.
+     */
+    public DefaultByteBuffersPool(int capacity) {
+        this.capacity = capacity;
+
+        queue = new ConcurrentLinkedQueue<>();
+    }
+
+    @Override
+    public @Nullable ByteBuffer borrow() {
+        ByteBuffer buffer = queue.poll();
+
+        if (buffer != null) {
+            return buffer;
+        }
+
+        if (size.get() < capacity && size.getAndIncrement() < capacity) {

Review Comment:
   doesn't look atomic and race-safe



##########
modules/raft/src/main/java/org/apache/ignite/internal/raft/util/DefaultByteBuffersPool.java:
##########
@@ -0,0 +1,72 @@
+/*
+ * 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.ignite.internal.raft.util;
+
+import java.nio.ByteBuffer;
+import java.util.Queue;
+import java.util.concurrent.ConcurrentLinkedQueue;
+import java.util.concurrent.atomic.AtomicInteger;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Basic pool implementation with limited capacity.
+ */
+public class DefaultByteBuffersPool implements ByteBuffersPool {
+    /** Max pool size. */
+    private final int capacity;
+
+    /** Queue with cached buffers. */
+    private final Queue<ByteBuffer> queue;
+
+    /** Pool size. */
+    private final AtomicInteger size = new AtomicInteger();
+
+    /**
+     * Constructor.
+     *
+     * @param capacity Max pool size.
+     */
+    public DefaultByteBuffersPool(int capacity) {
+        this.capacity = capacity;
+
+        queue = new ConcurrentLinkedQueue<>();
+    }
+
+    @Override
+    public @Nullable ByteBuffer borrow() {
+        ByteBuffer buffer = queue.poll();
+
+        if (buffer != null) {
+            return buffer;
+        }
+
+        if (size.get() < capacity && size.getAndIncrement() < capacity) {
+            return 
ByteBuffer.allocate(DEFAULT_BUFFER_SIZE).order(OptimizedMarshaller.ORDER);
+        }
+
+        return null;
+    }
+
+    @Override
+    public void release(ByteBuffer buffer) {
+        assert buffer.position() == 0;
+        assert buffer.capacity() <= MAX_CACHED_BUFFER_BYTES;

Review Comment:
   The maximum size can only be determined externally, it is also not entirely 
obvious and somehow needs to be reflected in the method above or in the 
documentation



##########
modules/raft/src/main/java/org/apache/ignite/internal/raft/util/DefaultByteBuffersPool.java:
##########
@@ -0,0 +1,72 @@
+/*
+ * 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.ignite.internal.raft.util;
+
+import java.nio.ByteBuffer;
+import java.util.Queue;
+import java.util.concurrent.ConcurrentLinkedQueue;
+import java.util.concurrent.atomic.AtomicInteger;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Basic pool implementation with limited capacity.
+ */
+public class DefaultByteBuffersPool implements ByteBuffersPool {
+    /** Max pool size. */
+    private final int capacity;
+
+    /** Queue with cached buffers. */
+    private final Queue<ByteBuffer> queue;
+
+    /** Pool size. */
+    private final AtomicInteger size = new AtomicInteger();
+
+    /**
+     * Constructor.
+     *
+     * @param capacity Max pool size.
+     */
+    public DefaultByteBuffersPool(int capacity) {
+        this.capacity = capacity;
+
+        queue = new ConcurrentLinkedQueue<>();
+    }
+
+    @Override
+    public @Nullable ByteBuffer borrow() {
+        ByteBuffer buffer = queue.poll();
+
+        if (buffer != null) {
+            return buffer;
+        }
+
+        if (size.get() < capacity && size.getAndIncrement() < capacity) {
+            return 
ByteBuffer.allocate(DEFAULT_BUFFER_SIZE).order(OptimizedMarshaller.ORDER);
+        }
+
+        return null;
+    }
+
+    @Override
+    public void release(ByteBuffer buffer) {
+        assert buffer.position() == 0;
+        assert buffer.capacity() <= MAX_CACHED_BUFFER_BYTES;
+
+        queue.add(buffer);

Review Comment:
   in the queue it turns out it may be larger than the maximum size, a little 
strange



##########
modules/raft/src/main/java/org/apache/ignite/internal/raft/util/DefaultByteBuffersPool.java:
##########
@@ -0,0 +1,72 @@
+/*
+ * 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.ignite.internal.raft.util;
+
+import java.nio.ByteBuffer;
+import java.util.Queue;
+import java.util.concurrent.ConcurrentLinkedQueue;
+import java.util.concurrent.atomic.AtomicInteger;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Basic pool implementation with limited capacity.
+ */
+public class DefaultByteBuffersPool implements ByteBuffersPool {
+    /** Max pool size. */
+    private final int capacity;
+
+    /** Queue with cached buffers. */
+    private final Queue<ByteBuffer> queue;
+
+    /** Pool size. */
+    private final AtomicInteger size = new AtomicInteger();
+
+    /**
+     * Constructor.
+     *
+     * @param capacity Max pool size.
+     */
+    public DefaultByteBuffersPool(int capacity) {
+        this.capacity = capacity;
+
+        queue = new ConcurrentLinkedQueue<>();
+    }
+
+    @Override
+    public @Nullable ByteBuffer borrow() {
+        ByteBuffer buffer = queue.poll();
+
+        if (buffer != null) {
+            return buffer;
+        }
+
+        if (size.get() < capacity && size.getAndIncrement() < capacity) {
+            return 
ByteBuffer.allocate(DEFAULT_BUFFER_SIZE).order(OptimizedMarshaller.ORDER);

Review Comment:
   It’s a little unclear to me that it should create a buffer with a default 
size, can it be passed as an argument?



##########
modules/raft/src/main/java/org/apache/ignite/internal/raft/util/OptimizedMarshaller.java:
##########
@@ -100,15 +110,36 @@ public byte[] marshall(Object o) {
             }
 
             buffer = expandBuffer(buffer);
+
+            if (buffer.capacity() <= MAX_CACHED_BUFFER_BYTES && poolBuffer != 
null) {
+                poolBuffer = buffer;
+            } else if (poolBuffer != null) {
+                poolBuffer.position(0);
+                pool.release(poolBuffer);
+
+                poolBuffer = null;
+            }
         }
 
+        // Prevent holding the reference for too long.
+        stream.setBuffer(EMPTY_BUFFER);
+
         byte[] result = Arrays.copyOf(buffer.array(), buffer.position());
 
-        buffer.position(0);
+        if (poolBuffer != null) {
+            poolBuffer.position(0);
+            pool.release(poolBuffer);
+        }
 
         return result;
     }
 
+    /**
+     * Invoked on empty buffer, before writing any data to it.
+     */

Review Comment:
   ```suggestion
       /** Invoked on empty buffer, before writing any data to it. */
   ```



##########
modules/raft/src/main/java/org/apache/ignite/internal/raft/util/DefaultByteBuffersPool.java:
##########
@@ -0,0 +1,72 @@
+/*
+ * 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.ignite.internal.raft.util;
+
+import java.nio.ByteBuffer;
+import java.util.Queue;
+import java.util.concurrent.ConcurrentLinkedQueue;
+import java.util.concurrent.atomic.AtomicInteger;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Basic pool implementation with limited capacity.
+ */
+public class DefaultByteBuffersPool implements ByteBuffersPool {
+    /** Max pool size. */
+    private final int capacity;
+
+    /** Queue with cached buffers. */
+    private final Queue<ByteBuffer> queue;
+
+    /** Pool size. */
+    private final AtomicInteger size = new AtomicInteger();
+
+    /**
+     * Constructor.
+     *
+     * @param capacity Max pool size.
+     */
+    public DefaultByteBuffersPool(int capacity) {
+        this.capacity = capacity;
+
+        queue = new ConcurrentLinkedQueue<>();
+    }
+
+    @Override
+    public @Nullable ByteBuffer borrow() {
+        ByteBuffer buffer = queue.poll();
+
+        if (buffer != null) {
+            return buffer;
+        }
+
+        if (size.get() < capacity && size.getAndIncrement() < capacity) {
+            return 
ByteBuffer.allocate(DEFAULT_BUFFER_SIZE).order(OptimizedMarshaller.ORDER);

Review Comment:
   It’s a little unclear to me that it should create a buffer with a default 
size, can it be passed as an argument?



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to