Skip to content

Commit

Permalink
Cleanup commit
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Alfonsi <[email protected]>
  • Loading branch information
Peter Alfonsi committed Jan 2, 2024
1 parent b46d779 commit 79e81a7
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ public byte[] serialize(IndicesRequestCache.Key object) {

@Override
public IndicesRequestCache.Key deserialize(byte[] bytes) {
if (bytes == null) {
return null;
}
try {
BytesStreamInput is = new BytesStreamInput(bytes, 0, bytes.length);
return irc.new Key(is);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import org.opensearch.common.util.concurrent.ConcurrentCollections;
import org.opensearch.core.common.bytes.BytesReference;
import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.core.common.io.stream.StreamOutput;
import org.opensearch.core.common.io.stream.Writeable;
import org.opensearch.core.common.unit.ByteSizeValue;

Expand Down Expand Up @@ -332,6 +333,13 @@ public int hashCode() {
result = 31 * result + value.hashCode();
return result;
}

@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeOptionalWriteable(entity);
out.writeOptionalString(readerCacheKeyId);
out.writeBytesReference(value);
}
}

private class CleanupKey implements IndexReader.ClosedListener {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.common.cache.tier;

import org.opensearch.common.Randomness;
import org.opensearch.common.bytes.ReleasableBytesReference;
import org.opensearch.common.util.BigArrays;
import org.opensearch.common.util.PageCacheRecycler;
import org.opensearch.core.common.bytes.BytesArray;
import org.opensearch.core.common.bytes.BytesReference;
import org.opensearch.core.common.bytes.CompositeBytesReference;
import org.opensearch.core.common.util.ByteArray;
import org.opensearch.test.OpenSearchTestCase;

import java.util.Random;

public class BytesReferenceSerializerTests extends OpenSearchTestCase {
public void testEquality() throws Exception {
BytesReferenceSerializer ser = new BytesReferenceSerializer();
// Test that values are equal before and after serialization, for each implementation of BytesReference.
byte[] bytesValue = new byte[1000];
Random rand = Randomness.get();
rand.nextBytes(bytesValue);

BytesReference ba = new BytesArray(bytesValue);
byte[] serialized = ser.serialize(ba);
assertTrue(ser.equals(ba, serialized));
BytesReference deserialized = ser.deserialize(serialized);
assertEquals(ba, deserialized);

ba = new BytesArray(new byte[] {});
serialized = ser.serialize(ba);
assertTrue(ser.equals(ba, serialized));
deserialized = ser.deserialize(serialized);
assertEquals(ba, deserialized);

BytesReference cbr = CompositeBytesReference.of(new BytesArray(bytesValue), new BytesArray(bytesValue));
serialized = ser.serialize(cbr);
assertTrue(ser.equals(cbr, serialized));
deserialized = ser.deserialize(serialized);
assertEquals(cbr, deserialized);

// We need the PagedBytesReference to be larger than the page size (16 KB) in order to actually create it
byte[] pbrValue = new byte[PageCacheRecycler.PAGE_SIZE_IN_BYTES * 2];
rand.nextBytes(pbrValue);
ByteArray arr = BigArrays.NON_RECYCLING_INSTANCE.newByteArray(pbrValue.length);
arr.set(0L, pbrValue, 0, pbrValue.length);
assert !arr.hasArray();
BytesReference pbr = BytesReference.fromByteArray(arr, pbrValue.length);
serialized = ser.serialize(pbr);
assertTrue(ser.equals(pbr, serialized));
deserialized = ser.deserialize(serialized);
assertEquals(pbr, deserialized);

BytesReference rbr = new ReleasableBytesReference(new BytesArray(bytesValue), ReleasableBytesReference.NO_OP);
serialized = ser.serialize(rbr);
assertTrue(ser.equals(rbr, serialized));
deserialized = ser.deserialize(serialized);
assertEquals(rbr, deserialized);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@
public class IRCKeyWriteableSerializerTests extends OpenSearchSingleNodeTestCase {

public void testSerializer() throws Exception {
ClusterSettings dummyClusterSettings = new ClusterSettings(Settings.EMPTY, ClusterSettings.BUILT_IN_CLUSTER_SETTINGS);
IndicesService indicesService = getInstanceFromNode(IndicesService.class);
IndicesRequestCache irc = new IndicesRequestCache(Settings.EMPTY, indicesService, dummyClusterSettings);
IndicesRequestCache irc = new IndicesRequestCache(Settings.EMPTY, indicesService);
IndexService indexService = createIndex("test");
IndexShard indexShard = indexService.getShardOrNull(0);
IndicesService.IndexShardCacheEntity entity = indicesService.new IndexShardCacheEntity(indexShard);
Expand Down

0 comments on commit 79e81a7

Please sign in to comment.