فهرست منبع

[ci fw-only Java/baseio] update to 3.2.9.beta5 (#4381)

Kai Wang 6 سال پیش
والد
کامیت
bc0f373554

+ 16 - 4
frameworks/Java/baseio/baseio-http-lite.dockerfile

@@ -4,7 +4,19 @@ COPY pom.xml pom.xml
 COPY src src
 RUN mvn compile assembly:single -q
 
-FROM openjdk:10-jre-slim
-WORKDIR /baseio
-COPY --from=maven /baseio/target/baseio-example-0.1-jar-with-dependencies.jar app.jar
-CMD ["java", "-server", "-XX:+UseNUMA", "-Dlite=true", "-Dcore=1", "-Dframe=16", "-DreadBuf=512", "-Dpool=true", "-Ddirect=true", "-Dlevel=1", "-Dread=false", "-XX:+UseParallelGC", "-XX:+AggressiveOpts", "-jar", "app.jar"]
+CMD java                       \
+    -server                    \
+    -XX:+UseNUMA               \
+    -XX:+UseParallelGC         \
+    -XX:+AggressiveOpts        \
+    -Dlite=true                \
+    -Dcore=1                   \
+    -Dframe=16                 \
+    -DreadBuf=512              \
+    -Dpool=true                \
+    -Ddirect=true              \
+    -Dlevel=1                  \
+    -Dread=false               \
+    -Depoll=true               \
+    -DunsafeBuf=true           \
+    -jar /baseio/target/baseio-example-0.1-jar-with-dependencies.jar

+ 16 - 4
frameworks/Java/baseio/baseio.dockerfile

@@ -4,7 +4,19 @@ COPY pom.xml pom.xml
 COPY src src
 RUN mvn compile assembly:single -q
 
-FROM openjdk:10-jre-slim
-WORKDIR /baseio
-COPY --from=maven /baseio/target/baseio-example-0.1-jar-with-dependencies.jar app.jar
-CMD ["java", "-server", "-XX:+UseNUMA", "-Dlite=false", "-Dcore=1", "-Dframe=16", "-DreadBuf=512", "-Dpool=true", "-Ddirect=true", "-Dlevel=1", "-Dread=false", "-XX:+UseParallelGC", "-XX:+AggressiveOpts", "-jar", "app.jar"]
+CMD java                       \
+    -server                    \
+    -XX:+UseNUMA               \
+    -XX:+UseParallelGC         \
+    -XX:+AggressiveOpts        \
+    -Dlite=false               \
+    -Dcore=1                   \
+    -Dframe=16                 \
+    -DreadBuf=512              \
+    -Dpool=true                \
+    -Ddirect=true              \
+    -Dlevel=1                  \
+    -Dread=false               \
+    -Depoll=true               \
+    -DunsafeBuf=true           \
+    -jar /baseio/target/baseio-example-0.1-jar-with-dependencies.jar

+ 57 - 57
frameworks/Java/baseio/pom.xml

@@ -1,68 +1,68 @@
 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 
-	<modelVersion>4.0.0</modelVersion>
+    <modelVersion>4.0.0</modelVersion>
 
-	<groupId>com.techempower</groupId>
-	<artifactId>baseio-example</artifactId>
-	<version>0.1</version>
+    <groupId>com.techempower</groupId>
+    <artifactId>baseio-example</artifactId>
+    <version>0.1</version>
 
-	<properties>
-		<maven.compiler.source>1.8</maven.compiler.source>
-		<maven.compiler.target>1.8</maven.compiler.target>
-	</properties>
+    <properties>
+        <maven.compiler.source>1.8</maven.compiler.source>
+        <maven.compiler.target>1.8</maven.compiler.target>
+    </properties>
 
-	<packaging>jar</packaging>
+    <packaging>jar</packaging>
 
-	<dependencies>
+    <dependencies>
 
-		<dependency>
-			<groupId>com.firenio</groupId>
-			<artifactId>baseio-all</artifactId>
-			<version>3.2.9-BETA-4</version>
-		</dependency>
+        <dependency>
+            <groupId>com.firenio</groupId>
+            <artifactId>baseio-all</artifactId>
+            <version>3.2.9.beta5</version>
+        </dependency>
 
-		<dependency>
-			<groupId>com.alibaba</groupId>
-			<artifactId>fastjson</artifactId>
-			<version>1.2.51</version>
-		</dependency>
+        <dependency>
+            <groupId>com.jsoniter</groupId>
+            <artifactId>jsoniter</artifactId>
+            <version>0.9.23</version>
+        </dependency>
 
-	</dependencies>
+    </dependencies>
 
-	<build>
-		<plugins>
-			<plugin>
-				<inherited>true</inherited>
-				<groupId>org.apache.maven.plugins</groupId>
-				<artifactId>maven-compiler-plugin</artifactId>
-				<version>3.8.0</version>
-				<configuration>
-					<debug>false</debug>
-				</configuration>
-			</plugin>
-			<plugin>
-				<artifactId>maven-assembly-plugin</artifactId>
-				<configuration>
-					<archive>
-						<manifest>
-							<mainClass>hello.TestHttpLoadServer</mainClass>
-						</manifest>
-					</archive>
-					<descriptorRefs>
-						<descriptorRef>jar-with-dependencies</descriptorRef>
-					</descriptorRefs>
-				</configuration>
-				<executions>
-					<execution>
-						<id>make-assembly</id> <!-- this is used for inheritance merges -->
-						<phase>package</phase> <!-- bind to the packaging phase -->
-						<goals>
-							<goal>single</goal>
-						</goals>
-					</execution>
-				</executions>
-			</plugin>
-		</plugins>
-	</build>
+    <build>
+        <plugins>
+            <plugin>
+                <inherited>true</inherited>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-compiler-plugin</artifactId>
+                <version>3.8.0</version>
+                <configuration>
+                    <debug>false</debug>
+                </configuration>
+            </plugin>
+            <plugin>
+                <artifactId>maven-assembly-plugin</artifactId>
+                <configuration>
+                    <archive>
+                        <manifest>
+                            <mainClass>hello.TestHttpLoadServer</mainClass>
+                        </manifest>
+                    </archive>
+                    <descriptorRefs>
+                        <descriptorRef>jar-with-dependencies</descriptorRef>
+                    </descriptorRefs>
+                </configuration>
+                <executions>
+                    <execution>
+                        <id>make-assembly</id> <!-- this is used for inheritance merges -->
+                        <phase>package</phase> <!-- bind to the packaging phase -->
+                        <goals>
+                            <goal>single</goal>
+                        </goals>
+                    </execution>
+                </executions>
+            </plugin>
+        </plugins>
+    </build>
 </project>

+ 72 - 41
frameworks/Java/baseio/src/main/java/hello/TestHttpLoadServer.java

@@ -15,43 +15,66 @@
  */
 package hello;
 
-import com.alibaba.fastjson.JSON;
+import java.io.IOException;
+import java.util.Arrays;
+
 import com.firenio.baseio.Options;
-import com.firenio.baseio.buffer.ByteBuf;
 import com.firenio.baseio.codec.http11.HttpCodec;
 import com.firenio.baseio.codec.http11.HttpCodecLite;
 import com.firenio.baseio.codec.http11.HttpFrameLite;
 import com.firenio.baseio.codec.http11.HttpStatic;
 import com.firenio.baseio.codec.http11.HttpStatus;
-import com.firenio.baseio.common.Encoding;
 import com.firenio.baseio.common.Util;
+import com.firenio.baseio.component.Channel;
 import com.firenio.baseio.component.ChannelAcceptor;
+import com.firenio.baseio.component.ChannelEventListenerAdapter;
+import com.firenio.baseio.component.Frame;
 import com.firenio.baseio.component.IoEventHandle;
 import com.firenio.baseio.component.NioEventLoopGroup;
-import com.firenio.baseio.component.NioSocketChannel;
+import com.firenio.baseio.component.ProtocolCodec;
+import com.firenio.baseio.component.SocketOptions;
 import com.firenio.baseio.log.DebugUtil;
 import com.firenio.baseio.log.LoggerFactory;
-import com.firenio.baseio.protocol.Frame;
-import com.firenio.baseio.protocol.ProtocolCodec;
+import com.jsoniter.output.JsonStream;
+import com.jsoniter.output.JsonStreamPool;
+import com.jsoniter.spi.JsonException;
 
 public class TestHttpLoadServer {
 
-    static final byte[] STATIC_PLAINTEXT = "Hello, World!".getBytes(Encoding.UTF8);
-    static final byte[] STATIC_SERVER    = "baseio".getBytes();
+    static final byte[] STATIC_PLAINTEXT = "Hello, World!".getBytes();
+
+    static class Message {
+
+        private final String message;
+
+        public Message(String message) {
+            this.message = message;
+        }
+
+        public String getMessage() {
+            return message;
+        }
+
+    }
 
     public static void main(String[] args) throws Exception {
-        boolean lite = Util.isSystemTrue("lite");
-        boolean read = Util.isSystemTrue("read");
-        boolean pool = Util.isSystemTrue("pool");
-        boolean direct = Util.isSystemTrue("direct");
-        int core = Util.getProperty("core", 1);
-        int frame = Util.getProperty("frame", 0);
-        int level = Util.getProperty("level");
-        int readBuf = Util.getProperty("readBuf", 1);
+        boolean lite = Util.getBooleanProperty("lite");
+        boolean read = Util.getBooleanProperty("read");
+        boolean pool = Util.getBooleanProperty("pool");
+        boolean direct = Util.getBooleanProperty("direct");
+        boolean epoll = Util.getBooleanProperty("epoll");
+        boolean unsafeBuf = Util.getBooleanProperty("unsafeBuf");
+        int core = Util.getIntProperty("core", 1);
+        int frame = Util.getIntProperty("frame", 16);
+        int level = Util.getIntProperty("level", 1);
+        int readBuf = Util.getIntProperty("readBuf", 16);
         LoggerFactory.setEnableSLF4JLogger(false);
         LoggerFactory.setLogLevel(LoggerFactory.LEVEL_INFO);
+        Options.setBufAutoExpansion(false);
         Options.setDebugErrorLevel(level);
         Options.setChannelReadFirst(read);
+        Options.setEnableEpoll(epoll);
+        Options.setEnableUnsafeBuf(unsafeBuf);
         DebugUtil.info("lite: {}", lite);
         DebugUtil.info("read: {}", read);
         DebugUtil.info("pool: {}", pool);
@@ -64,56 +87,64 @@ public class TestHttpLoadServer {
         IoEventHandle eventHandle = new IoEventHandle() {
 
             @Override
-            public void accept(NioSocketChannel ch, Frame frame) throws Exception {
+            public void accept(Channel ch, Frame frame) throws Exception {
                 HttpFrameLite f = (HttpFrameLite) frame;
                 String action = f.getRequestURL();
-
                 if ("/plaintext".equals(action)) {
-                    f.write(STATIC_PLAINTEXT);
+                    f.setContent(STATIC_PLAINTEXT);
                     f.setContentType(HttpStatic.text_plain_bytes);
                 } else if ("/json".equals(action)) {
-                    f.write(JSON.toJSONString(new Message("Hello, World!")), ch);
+                    f.setContent(serializeMsg(new Message("Hello, World!")));
                     f.setContentType(HttpStatic.application_json_bytes);
                 } else {
-                    f.write("404,page not found!", ch);
+                    f.setContent("404,page not found!".getBytes());
                     f.setStatus(HttpStatus.C404);
                 }
-                ByteBuf buf = ch.encode(f);
-                ch.flush(buf);
+                ch.writeAndFlush(f);
                 ch.release(f);
             }
 
         };
 
-        ProtocolCodec codec = lite ? new HttpCodecLite("baseio", 1024 * frame)
-                : new HttpCodec("baseio", 1024 * frame);
+        ProtocolCodec codec;
+        if (lite) {
+            codec = new HttpCodecLite("baseio", 1024 * 16);
+        } else {
+            codec = new HttpCodec("baseio", 1024 * 16);
+        }
         NioEventLoopGroup group = new NioEventLoopGroup();
-        group.setEnableMemoryPool(pool);
-        group.setEnableMemoryPoolDirect(direct);
+        ChannelAcceptor context = new ChannelAcceptor(group, 8080);
         group.setMemoryPoolCapacity(1024 * 128);
-        group.setChannelReadBuffer(1024 * readBuf);
         group.setMemoryPoolUnit(256);
         group.setWriteBuffers(32);
+        group.setChannelReadBuffer(1024 * readBuf);
         group.setEventLoopSize(Util.availableProcessors() * core);
         group.setConcurrentFrameStack(false);
-        ChannelAcceptor context = new ChannelAcceptor(group, 8080);
-        context.setProtocolCodec(codec);
+        context.addProtocolCodec(codec);
+        context.addChannelEventListener(new ChannelEventListenerAdapter() {
+
+            @Override
+            public void channelOpened(Channel ch) throws Exception {
+                ch.setOption(SocketOptions.TCP_NODELAY, 1);
+                ch.setOption(SocketOptions.TCP_QUICKACK, 1);
+                ch.setOption(SocketOptions.SO_KEEPALIVE, 0);
+            }
+        });
         context.setIoEventHandle(eventHandle);
         context.bind();
     }
 
-    static class Message {
-
-        private final String message;
-
-        public Message(String message) {
-            this.message = message;
-        }
-
-        public String getMessage() {
-            return message;
+    private static byte[] serializeMsg(Message obj) {
+        JsonStream stream = JsonStreamPool.borrowJsonStream();
+        try {
+            stream.reset(null);
+            stream.writeVal(Message.class, obj);
+            return Arrays.copyOfRange(stream.buffer().data(), 0, stream.buffer().tail());
+        } catch (IOException e) {
+            throw new JsonException(e);
+        } finally {
+            JsonStreamPool.returnJsonStream(stream);
         }
-
     }
 
 }