Skip to content

Commit

Permalink
Merge branch 'master' into added_list_to_jcloud
Browse files Browse the repository at this point in the history
  • Loading branch information
alebastrov authored Apr 25, 2024
2 parents a6fbd46 + 2fbc081 commit ecc02dc
Show file tree
Hide file tree
Showing 19 changed files with 493 additions and 322 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
import oap.logstream.disk.DiskLoggerBackend;
import oap.logstream.net.client.SocketLoggerBackend;
import oap.logstream.net.server.SocketLoggerServer;
import oap.message.server.MessageHttpHandler;
import oap.message.client.MessageSender;
import oap.message.server.MessageHttpHandler;
import oap.template.BinaryUtils;
import oap.template.Types;
import oap.testng.Fixtures;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,10 @@
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

@Slf4j
@ToString
Expand Down Expand Up @@ -376,6 +378,10 @@ private Messages.MessageInfo onOkRespone( Messages.MessageInfo messageInfo, Clie
}

public void syncMemory() {
syncMemory( -1 );
}

public void syncMemory( long timeoutMs ) {
if( getReadyMessages() + getRetryMessages() + getInProgressMessages() > 0 )
log.trace( "[{}] sync ready {} retry {} inprogress {} ...",
uniqueName, getReadyMessages(), getRetryMessages(), getInProgressMessages() );
Expand All @@ -401,6 +407,14 @@ public void syncMemory() {
log.trace( "[{}] message {}... done", uniqueName, mi.message.md5 );
return null;
} );

if( timeoutMs >= 0 ) {
try {
future.get( timeoutMs, TimeUnit.MILLISECONDS );
} catch( InterruptedException | TimeoutException | ExecutionException e ) {
log.error( e.getMessage(), e );
}
}
}

if( isGlobalIoRetryTimeout( now ) ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,20 @@ public short run( int version, String hostName, int size, byte[] data, String md

accessCount.incrementAndGet();
if( throwUnknownError > 0 ) {
log.debug( "throwUnknownError {} noRetry {}", throwUnknownError, noRetry );
throwUnknownError -= 1;
if( noRetry )
if( noRetry ) {
log.debug( "RuntimeException -> unknown error" );
throw new RuntimeException( "unknown error" );
else
} else {
log.debug( "MessageProtocol.STATUS_UNKNOWN_ERROR" );
return MessageProtocol.STATUS_UNKNOWN_ERROR;
}
}

if( status == MessageProtocol.STATUS_OK )
if( status == MessageProtocol.STATUS_OK ) {
messages.add( new TestMessage( version, md5, new String( data, UTF_8 ) ) );
}

return status;
}
Expand Down

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions oap-message/oap-message-test/src/test/resources/logback-test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>

<logger name="io.undertow" level="WARN"/>
<logger name="org" level="WARN"/>

<root level="TRACE">
<appender-ref ref="STDOUT"/>
</root>
</configuration>
13 changes: 13 additions & 0 deletions oap-stdlib-test/src/test/java/oap/io/IoStreamsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import static oap.testng.Asserts.assertFile;
import static oap.testng.Asserts.pathOfTestResource;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

public class IoStreamsTest extends Fixtures {
private final TestDirectoryFixture testDirectoryFixture;
Expand Down Expand Up @@ -151,4 +152,16 @@ public void compressionLevel() {
System.out.println( encoding + ":\t" + content.length() + " -> " + path.toFile().length() );
}
}

@Test
public void testThrowIfFileExists() throws IOException {
Path testFile = testDirectoryFixture.testPath( "testFile" );
testFile.toFile().createNewFile();

assertThatThrownBy( () -> {
try( var _ = IoStreams.out( testFile, new IoStreams.OutOptions().withThrowIfFileExists( true ) ) ) {
System.out.println( "!" );
}
} ).isInstanceOf( oap.io.FileExistsException.class );
}
}
3 changes: 1 addition & 2 deletions oap-stdlib-test/src/test/java/oap/util/ThrowablesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@
import org.testng.annotations.Test;

import java.io.IOException;
import java.io.UncheckedIOException;

public class ThrowablesTest {
@Test( expectedExceptions = UncheckedIOException.class, expectedExceptionsMessageRegExp = "java.io.IOException: test" )
@Test( expectedExceptions = oap.io.IOException.class, expectedExceptionsMessageRegExp = "java.io.IOException: test" )
public void propagateIOException() {
throw Throwables.propagate( new IOException( "test" ) );
}
Expand Down
13 changes: 13 additions & 0 deletions oap-stdlib/src/main/java/oap/io/FileExistsException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package oap.io;

import java.io.File;

public class FileExistsException extends IOException {
public FileExistsException( File file ) {
super( "File " + file + " exists" );
}

public FileExistsException( IOException cause ) {
super( cause );
}
}
11 changes: 11 additions & 0 deletions oap-stdlib/src/main/java/oap/io/FileNotFoundException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package oap.io;

public class FileNotFoundException extends IOException {
public FileNotFoundException( String message ) {
super( message );
}

public FileNotFoundException( java.io.FileNotFoundException cause ) {
super( cause );
}
}
23 changes: 11 additions & 12 deletions oap-stdlib/src/main/java/oap/io/Files.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UncheckedIOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.CopyOption;
Expand Down Expand Up @@ -509,7 +508,7 @@ public static void rename( Path sourcePath, Path destPath ) {
ensureFile( destPath );
java.nio.file.Files.move( sourcePath, destPath, ATOMIC_MOVE, REPLACE_EXISTING );
} catch( IOException e ) {
throw new UncheckedIOException( "cannot rename " + sourcePath + " to " + destPath, e );
throw new oap.io.IOException( "cannot rename " + sourcePath + " to " + destPath, e );
}
}

Expand All @@ -526,7 +525,7 @@ public static Path ensureDirectory( Path path ) {
}
}

public static void move( Path source, Path target, CopyOption... options ) {
public static void move( Path source, Path target, CopyOption... options ) throws oap.io.IOException {
try {
ensureFile( target );
java.nio.file.Files.move( source, target, options );
Expand All @@ -535,39 +534,39 @@ public static void move( Path source, Path target, CopyOption... options ) {
}
}

public static void setPosixPermissions( Path path, Set<PosixFilePermission> permissions ) {
public static void setPosixPermissions( Path path, Set<PosixFilePermission> permissions ) throws oap.io.IOException {
try {
java.nio.file.Files.setPosixFilePermissions( path, permissions );
} catch( IOException e ) {
throw Throwables.propagate( e );
}
}

public static void setPosixPermissions( Path path, PosixFilePermission... permissions ) {
public static void setPosixPermissions( Path path, PosixFilePermission... permissions ) throws oap.io.IOException {
setPosixPermissions( path, Sets.of( permissions ) );
}

public static Set<PosixFilePermission> getPosixPermissions( Path path ) {
public static Set<PosixFilePermission> getPosixPermissions( Path path ) throws oap.io.IOException {
try {
return java.nio.file.Files.getPosixFilePermissions( path, LinkOption.NOFOLLOW_LINKS );
} catch( IOException e ) {
throw Throwables.propagate( e );
}
}

public static boolean isDirectoryEmpty( Path directory ) {
public static boolean isDirectoryEmpty( Path directory ) throws oap.io.IOException {
try( DirectoryStream<Path> dirStream = java.nio.file.Files.newDirectoryStream( directory ) ) {
return !dirStream.iterator().hasNext();
} catch( IOException e ) {
throw Throwables.propagate( e );
}
}

public static void setLastModifiedTime( Path path, DateTime dateTime ) {
public static void setLastModifiedTime( Path path, DateTime dateTime ) throws oap.io.IOException {
setLastModifiedTime( path, dateTime.getMillis() );
}

public static void setLastModifiedTime( Path path, long ms ) {
public static void setLastModifiedTime( Path path, long ms ) throws oap.io.IOException {
try {
java.nio.file.Files.setLastModifiedTime( path, FileTime.fromMillis( ms ) );
} catch( IOException e ) {
Expand Down Expand Up @@ -654,7 +653,7 @@ public static boolean fileNotEmpty( Path path ) {
return isFileNotEmpty( path );
}

public static boolean isFileNotEmpty( final Path path ) {
public static boolean isFileNotEmpty( final Path path ) throws oap.io.IOException {
try( InputStream is = IoStreams.in( path );
InputStreamReader isr = new InputStreamReader( is );
BufferedReader reader = new BufferedReader( isr ) ) {
Expand All @@ -670,15 +669,15 @@ public static boolean exists( Path path ) {
return java.nio.file.Files.exists( path );
}

public static long getLastModifiedTime( Path path ) {
public static long getLastModifiedTime( Path path ) throws oap.io.IOException {
try {
return java.nio.file.Files.getLastModifiedTime( path ).toMillis();
} catch( IOException e ) {
throw Throwables.propagate( e );
}
}

public static boolean createFile( Path file ) {
public static boolean createFile( Path file ) throws oap.io.IOException {
try {
java.nio.file.Files.createFile( file );
return true;
Expand Down
18 changes: 18 additions & 0 deletions oap-stdlib/src/main/java/oap/io/IOException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package oap.io;

public class IOException extends RuntimeException {
public IOException() {
}

public IOException( String message ) {
super( message );
}

public IOException( String message, Throwable cause ) {
super( message, cause );
}

public IOException( Throwable cause ) {
super( cause );
}
}
Loading

0 comments on commit ecc02dc

Please sign in to comment.