Skip to content

Commit

Permalink
optimize: support jdk9+ compile code (sofastack#772)
Browse files Browse the repository at this point in the history
* optimize: support jdk9+ compile code
  • Loading branch information
funky-eyes authored Apr 19, 2022
1 parent e804c9d commit b9ce898
Show file tree
Hide file tree
Showing 23 changed files with 176 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import com.alipay.sofa.jraft.rpc.RpcResponseClosureAdapter;
import com.alipay.sofa.jraft.rpc.RpcUtils;
import com.alipay.sofa.jraft.storage.snapshot.SnapshotReader;
import com.alipay.sofa.jraft.util.BufferUtils;
import com.alipay.sofa.jraft.util.ByteBufferCollector;
import com.alipay.sofa.jraft.util.OnlyForTest;
import com.alipay.sofa.jraft.util.Recyclable;
Expand Down Expand Up @@ -1643,7 +1644,7 @@ private boolean sendEntries(final long nextSendingIndex) {
dataBuf.put(b);
}
final ByteBuffer buf = dataBuf.getBuffer();
buf.flip();
BufferUtils.flip(buf);
rb.setData(ZeroByteStringHelper.wrap(buf));
}
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.alipay.sofa.jraft.entity;

import java.nio.ByteBuffer;
import com.alipay.sofa.jraft.util.BufferUtils;

/**
* User log entry.
Expand Down Expand Up @@ -55,7 +56,7 @@ public void setData(ByteBuffer data) {
}

public void reset() {
this.data.clear();
BufferUtils.clear(this.data);
this.index = 0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.alipay.sofa.jraft.entity.codec.LogEntryDecoder;
import com.alipay.sofa.jraft.util.AsciiStringUtil;
import com.alipay.sofa.jraft.util.Bits;
import com.alipay.sofa.jraft.util.BufferUtils;

/**
* V1 log entry decoder
Expand Down Expand Up @@ -108,7 +109,7 @@ public void decode(final LogEntry log, final byte[] content) {
final int len = content.length - pos;
ByteBuffer data = ByteBuffer.allocate(len);
data.put(content, pos, len);
data.flip();
BufferUtils.flip(data);
log.setData(data);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.alipay.sofa.jraft.rpc.RpcRequests.GetFileRequest;
import com.alipay.sofa.jraft.rpc.RpcRequests.GetFileResponse;
import com.alipay.sofa.jraft.storage.io.FileReader;
import com.alipay.sofa.jraft.util.BufferUtils;
import com.alipay.sofa.jraft.util.ByteBufferCollector;
import com.alipay.sofa.jraft.util.OnlyForTest;
import com.alipay.sofa.jraft.util.RpcFactoryHelper;
Expand Down Expand Up @@ -108,7 +109,7 @@ public Message handleGetFile(final GetFileRequest request, final RpcRequestClosu
responseBuilder.setReadSize(read);
responseBuilder.setEof(read == FileReader.EOF);
final ByteBuffer buf = dataBuffer.getBuffer();
buf.flip();
BufferUtils.flip(buf);
if (!buf.hasRemaining()) {
// skip empty data
responseBuilder.setData(ByteString.EMPTY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import com.alipay.sofa.jraft.storage.impl.RocksDBLogStorage.WriteContext;
import com.alipay.sofa.jraft.storage.log.SegmentFile.SegmentFileOptions;
import com.alipay.sofa.jraft.util.Bits;
import com.alipay.sofa.jraft.util.BufferUtils;
import com.alipay.sofa.jraft.util.BytesUtil;
import com.alipay.sofa.jraft.util.OnlyForTest;
import com.alipay.sofa.jraft.util.Utils;
Expand Down Expand Up @@ -97,7 +98,7 @@ ByteBuffer encode() {
buffer.put(MAGIC);
buffer.putLong(this.firstLogIndex);
buffer.putLong(RESERVED_FLAG);
buffer.flip();
BufferUtils.flip(buffer);
return buffer;
}

Expand Down Expand Up @@ -412,7 +413,7 @@ public void truncateSuffix(final int wrotePos, final long logIndex, final boolea
clear(wrotePos, sync);
this.wrotePos = wrotePos;
this.lastLogIndex = logIndex;
this.buffer.position(wrotePos);
BufferUtils.position(this.buffer, wrotePos);
LOG.info(
"Segment file {} truncate suffix from pos={}, then set lastLogIndex={}, oldWrotePos={}, newWrotePos={}",
this.path, wrotePos, logIndex, oldPos, this.wrotePos);
Expand Down Expand Up @@ -485,12 +486,12 @@ private boolean loadNewFile(final SegmentFileOptions opts) {
try (FileChannel fc = openFileChannel(true)) {
this.buffer = fc.map(MapMode.READ_WRITE, 0, this.size);
// Warmup mmap file
this.buffer.position(0);
this.buffer.limit(this.size);
BufferUtils.position(this.buffer, 0);
BufferUtils.limit(this.buffer, this.size);
saveHeader(true);

this.committedPos = this.wrotePos = HEADER_SIZE;
this.buffer.position(this.wrotePos);
BufferUtils.position(this.buffer, this.wrotePos);

assert (this.wrotePos == this.buffer.position());

Expand Down Expand Up @@ -527,7 +528,7 @@ private boolean tryRecoverExistsFile(final SegmentFileOptions opts) {
// A blank segment, we don't need to recover.
assert (!opts.recover);
this.committedPos = this.wrotePos = HEADER_SIZE;
this.buffer.position(this.wrotePos);
BufferUtils.position(this.buffer, this.wrotePos);
LOG.info("Segment file {} is blank, truncate it from {}.", this.path, HEADER_SIZE);
clear(this.wrotePos, opts.sync);
} else {
Expand All @@ -537,7 +538,7 @@ private boolean tryRecoverExistsFile(final SegmentFileOptions opts) {
}
} else {
this.wrotePos = opts.pos;
this.buffer.position(this.wrotePos);
BufferUtils.position(this.buffer, this.wrotePos);
}
assert (this.wrotePos == this.buffer.position());
this.committedPos = this.wrotePos;
Expand Down Expand Up @@ -591,25 +592,25 @@ private FileChannel openFileChannel(final boolean create) throws IOException {
boolean loadHeader() {
int oldPos = this.buffer.position();
try {
this.buffer.position(0);
BufferUtils.position(this.buffer, 0);
return this.header.decode(this.buffer.asReadOnlyBuffer());
} finally {
this.buffer.position(oldPos);
BufferUtils.position(this.buffer, oldPos);
}
}

void saveHeader(final boolean sync) {
int oldPos = this.buffer.position();
try {
this.buffer.position(0);
BufferUtils.position(this.buffer, 0);
final ByteBuffer headerBuf = this.header.encode();
assert (headerBuf.remaining() == HEADER_SIZE);
this.buffer.put(headerBuf);
if (sync) {
fsync(this.buffer);
}
} finally {
this.buffer.position(oldPos);
BufferUtils.position(this.buffer, oldPos);
}
}

Expand All @@ -620,7 +621,7 @@ private boolean recover(final SegmentFileOptions opts) throws IOException {
if (this.wrotePos < HEADER_SIZE) {
this.wrotePos = HEADER_SIZE;
}
this.buffer.position(this.wrotePos);
BufferUtils.position(this.buffer, this.wrotePos);
final long start = Utils.monotonicMs();
while (this.wrotePos < this.size) {
if (this.buffer.remaining() < RECORD_MAGIC_BYTES_SIZE) {
Expand Down Expand Up @@ -658,7 +659,7 @@ private boolean recover(final SegmentFileOptions opts) throws IOException {
truncateFile(opts.sync);
} else {
// Reach blank hole, rewind position.
this.buffer.position(this.buffer.position() - RECORD_MAGIC_BYTES_SIZE);
BufferUtils.position(this.buffer, this.buffer.position() - RECORD_MAGIC_BYTES_SIZE);
}
// Reach end or dirty magic bytes, we should break out.
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.alipay.sofa.jraft.storage.SnapshotThrottle;
import com.alipay.sofa.jraft.storage.io.LocalDirReader;
import com.alipay.sofa.jraft.storage.snapshot.Snapshot;
import com.alipay.sofa.jraft.util.BufferUtils;
import com.alipay.sofa.jraft.util.ByteBufferCollector;

/**
Expand Down Expand Up @@ -65,7 +66,7 @@ public int readFile(final ByteBufferCollector metaBufferCollector, final String
if (fileName.equals(Snapshot.JRAFT_SNAPSHOT_META_FILE)) {
final ByteBuffer metaBuf = this.metaTable.saveToByteBufferAsRemote();
// because bufRef will flip the buffer before using, so we must set the meta buffer position to it's limit.
metaBuf.position(metaBuf.limit());
BufferUtils.position(metaBuf, metaBuf.limit());
metaBufferCollector.setBuffer(metaBuf);
return EOF;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import com.alipay.sofa.jraft.rpc.RpcResponseClosureAdapter;
import com.alipay.sofa.jraft.rpc.RpcUtils;
import com.alipay.sofa.jraft.storage.SnapshotThrottle;
import com.alipay.sofa.jraft.util.BufferUtils;
import com.alipay.sofa.jraft.util.ByteBufferCollector;
import com.alipay.sofa.jraft.util.Endpoint;
import com.alipay.sofa.jraft.util.OnlyForTest;
Expand Down Expand Up @@ -198,7 +199,7 @@ private void onFinished() {
if (this.destBuf != null) {
final ByteBuffer buf = this.destBuf.getBuffer();
if (buf != null) {
buf.flip();
BufferUtils.flip(buf);
}
this.destBuf = null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* 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 com.alipay.sofa.jraft.util;

import java.nio.Buffer;

/**
* Explicit cast to {@link Buffer} parent buffer type. It resolves issues with covariant return types in Java 9+ for
* {@link java.nio.ByteBuffer} and {@link java.nio.CharBuffer}. Explicit casting resolves the NoSuchMethodErrors (e.g
* java.lang.NoSuchMethodError: java.nio.ByteBuffer.flip(I)Ljava/nio/ByteBuffer) when the project is compiled with
* newer Java version and run on Java 8.
* <p/>
* <a href="https://docs.oracle.com/javase/8/docs/api/java/nio/ByteBuffer.html">Java 8</a> doesn't provide override the
* following Buffer methods in subclasses:
*
* <pre>
* Buffer clear()
* Buffer flip()
* Buffer limit(int newLimit)
* Buffer mark()
* Buffer position(int newPosition)
* Buffer reset()
* Buffer rewind()
* </pre>
*
* <a href="https://docs.oracle.com/javase/9/docs/api/java/nio/ByteBuffer.html">Java 9</a> introduces the overrides in
* child classes (e.g the ByteBuffer), but the return type is the specialized one and not the abstract {@link Buffer}.
* So the code compiled with newer Java is not working on Java 8 unless a workaround with explicit casting is used.
*
* @author funkye
*/
public class BufferUtils {

/**
* @param buffer byteBuffer
*/
public static void flip(Buffer buffer) {
buffer.flip();
}

/**
* @param buffer byteBuffer
*/
public static void clear(Buffer buffer) {
buffer.clear();
}

/**
* @param buffer byteBuffer
*/
public static void limit(Buffer buffer, int newLimit) {
buffer.limit(newLimit);
}

/**
* @param buffer byteBuffer
*/
public static void mark(Buffer buffer) {
buffer.mark();
}

/**
* @param buffer byteBuffer
*/
public static void position(Buffer buffer, int newPosition) {
buffer.position(newPosition);
}

/**
* @param buffer byteBuffer
*/
public static void rewind(Buffer buffer) {
buffer.rewind();
}

/**
* @param buffer byteBuffer
*/
public static void reset(Buffer buffer) {
buffer.reset();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public boolean recycle() {
// If the size is too large, we should release it to avoid memory overhead
this.buffer = null;
} else {
this.buffer.clear();
BufferUtils.clear(this.buffer);
}
}
return recyclers.recycle(this, handle);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ public static long crc64(final ByteBuffer buf) {
return crc64(buf.array(), pos + buf.arrayOffset(), rem);
}
final byte[] b = new byte[rem];
buf.mark();
BufferUtils.mark(buf);
buf.get(b);
buf.reset();
BufferUtils.reset(buf);
return crc64(b);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ public static ByteBuffer allocate() {
public static ByteBuffer expandByteBufferAtLeast(final ByteBuffer buf, final int minLength) {
final int newCapacity = minLength > RAFT_DATA_BUF_SIZE ? minLength : RAFT_DATA_BUF_SIZE;
final ByteBuffer newBuf = ByteBuffer.allocate(buf.capacity() + newCapacity);
buf.flip();
BufferUtils.flip(buf);
newBuf.put(buf);
return newBuf;
}
Expand All @@ -291,7 +291,7 @@ public static ByteBuffer expandByteBufferAtLeast(final ByteBuffer buf, final int
public static ByteBuffer expandByteBufferAtMost(final ByteBuffer buf, final int maxLength) {
final int newCapacity = maxLength > RAFT_DATA_BUF_SIZE || maxLength <= 0 ? RAFT_DATA_BUF_SIZE : maxLength;
final ByteBuffer newBuf = ByteBuffer.allocate(buf.capacity() + newCapacity);
buf.flip();
BufferUtils.flip(buf);
newBuf.put(buf);
return newBuf;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

import java.nio.ByteBuffer;
import java.util.Arrays;
import com.alipay.sofa.jraft.util.BufferUtils;

import static java.lang.Character.MAX_SURROGATE;
import static java.lang.Character.MIN_HIGH_SURROGATE;
Expand Down Expand Up @@ -274,7 +275,7 @@ public static void encodeUtf8Direct(CharSequence in, ByteBuffer out) {
}
if (inIx == inLimit) {
// We're done, it was ASCII encoded.
out.position((int) (outIx - address));
BufferUtils.position(out, (int) (outIx - address));
return;
}

Expand Down Expand Up @@ -314,7 +315,7 @@ public static void encodeUtf8Direct(CharSequence in, ByteBuffer out) {
}

// All bytes have been encoded.
out.position((int) (outIx - address));
BufferUtils.position(out, (int) (outIx - address));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.nio.ReadOnlyBufferException;
import java.util.Arrays;

import com.alipay.sofa.jraft.util.BufferUtils;
import org.junit.Assert;
import org.junit.Test;

Expand Down Expand Up @@ -153,7 +154,7 @@ public void testSliceReadOnlyData() {
assertEquals("hella", new String(bs));

try {
readOnly.position(4);
BufferUtils.position(readOnly, 4);
readOnly.put((byte) 1);
fail();
} catch (ReadOnlyBufferException e) {
Expand Down
Loading

0 comments on commit b9ce898

Please sign in to comment.