Posts

Showing posts from February, 2020

ReentrantLock vs Synchronized - Performance comparison

Image
Recent experience performance problems caused by high concurrency, final positioning of the problem is that LinkedBlockingQueuethe performance is not, ultimately to reduce the competitive pressure of each Queue by creating multiple Queue. For the first time in my life, I encountered a situation where the JDK's own data structure could not meet the needs, and I was determined to study why. The pressure test is on a 40-core machine. Tomcat defaults to 200 threads. The sender sends 500 requests at about 1w QPS and requires a response of 999 quantiles at about 50ms. There is a task that writes to the database asynchronously in the code. In actual testing, more than 60% of the delay is in the write queue (in fact, the task is submitted to the ThreadPool). He began research LinkedBlockingQueueimplementation. Equivalent plus LinkedList LinkedBlockingQueue ordinary ReentrantLocklocking during operation. ReentrantLock (and other locks in Java) internally rely on CAS to achieve atomicit...

HTTP keep-alive

Image
HTTP keep-alive is also called HTTP persistent connection. It reduces the overhead of creating / closing multiple TCP connections by reusing one TCP connection to send / receive multiple HTTP requests. What is keep-alive? Keep-alive is a convention between the client and the server. If keep-alive is enabled, the server does not close the TCP connection after returning the response. Similarly, after receiving the response message, the client does not close the connection and sends the next The connection is reused when an HTTP request is made. In the HTTP / 1.0 protocol, if the request header contains: Connection: keep-alive It means that keep-alive is turned on, and the return header of the server will also contain the same content. In the HTTP / 1.1 protocol, keep-alive is turned on by default, unless it is explicitly turned off: Connection: close The purpose of the keep-alive technology is to reuse the same TCP connection between multiple HTTPs, thereby reducing the...

Spring Security - API token authentication implementation

Image
Common permission authentication is done by providing a "username password". There are some APIs in the business, and we want to verify them in the form of API Tokens. For example, adding a token /api?token=xxxxto the URL allows API access. The logic behind this design is that usernames and passwords have higher permissions, and API tokens can only give permissions to a certain subsystem. Spring Security  Java Servlet and Spring Security uses a design pattern Chain of Responsibility pattern . Simply put, they all define many filters, and each request will be processed by layers of filters and finally returned. Spring Security registers a filter in the filter chain of the servlet FilterChainProxy, which will proxy requests to multiple filter chains maintained by Spring Security itself, and each filter chain will match some URLs, as shown in the figure /foo/**, If it matches, the corresponding filter is executed. Filter chains are sequential, and a request will only ...

Java - ZGC (A Scalable Low-Latency Garbage Collector)

The Z Garbage Collector, also known as ZGC, is a scalable low latency garbage collector. It is designed to meet the following goals: Pause times do not exceed 10 ms Pause times do not increase with the heap or live-set size Handle heaps ranging from a few hundred megabytes to multi terabytes in size ZGC is a concurrent garbage collector, meaning that all heavy lifting work (marking, compaction, reference processing, string table cleaning, etc) is done while Java threads continue to execute. This greatly limits the negative impact that garbage collection has on application response times. ZGC is included as an experimental feature. To enable it, the -XX:+UnlockExperimentalVMOptions option will therefore need to be used in combination with the -XX:+UseZGC option. This experimental version of ZGC has the following limitations: It is only available on Linux/x64. Using compressed oops and/or compressed class points is not supported. The -XX:+UseCompressedOops and -...

Java Lazy Allocation of Compiler Threads

Command line flag -XX:+UseDynamicNumberOfCompilerThreads dynamically control compiler threads. The VM starts a large number of compiler threads on systems with many CPUs regardless of the available memory and the number of compilation requests. Because the threads consume memory even when they are idle (which is almost all of the time), this leads to an inefficient use of resources. To address this issue, the implementation has been changed to start only one compiler thread of each type during startup and to handle the start and shutdown of further threads dynamically. It is controlled by a new command line flag, which is on by default: -XX:+UseDynamicNumberOfCompilerThreads

Java Collection.toArray(IntFunction) Default Method

A new default method toArray(IntFunction) has been added to the java.util.Collection interface. This method allows the collection's elements to be transferred to a newly created array of a desired runtime type. The new method is an overload of the existing toArray(T[]) method that takes an array instance as an argument. The addition of the overloaded method creates a minor source incompatibility. Previously, code of the form coll.toArray(null) would always resolve to the existing toArray method. With the new overloaded method, this code is now ambiguous and will result in a compile-time error. (This is only a source incompatibility. Existing binaries are unaffected.) The ambiguous code should be changed to cast null to the desired array type, for example, toArray((Object[])null) or some other array type. Note that passing null to either toArray method is specified to throw NullPointerException.

What are the important changes in Java JDK 11?

These are few details about the changes described below are provided in JDK 11 Release Notes. The deployment stack, required for Applets and Web Start Applications, was deprecated in JDK 9 and has been removed in JDK 11. Without a deployment stack, the entire section of supported browsers has been removed from the list of supported configurations of JDK 11. Auto-update, which was available for JRE installations on Windows and macOS, is no longer available. In Windows and macOS, installing the JDK in previous releases optionally installed a JRE. In JDK 11, this is no longer an option. In this release, the JRE or Server JRE is no longer offered. Only the JDK is offered. Users can use jlink to create smaller custom runtimes. JavaFX is no longer included in the JDK. It is now available as a separate download from openjfx.io. Java Mission Control, which was shipped in JDK 7, 8, 9, and 10, is no longer included with the Oracle JDK. It is now a separate download. Previous releases...

Java FileSystems.newFileSystem Method

Three new methods have been added to java.nio.file.FileSystems newFileSystem(Path) newFileSystem(Path, Map<String, ?>) newFileSystem(Path, Map<String, ?>, ClassLoader) newFileSystem(Path, Map<String, ?>) creates a source (but not binary) compatibility issue for code that has been using the existing 2-arg newFileSystem(Path, ClassLoader) and specifying the class loader as null. For example, the following cannot be compiled because the reference to newFileSystem is ambiguous: FileSystem fs = FileSystems.newFileSystem(path, null); To avoid the ambiguous reference, this code needs to be modified to cast the second parameter to java.lang.ClassLoader.

What is the Differences Between Oracle JDK and Oracle's OpenJDK?

Differences Between Oracle JDK and Oracle's OpenJDK: Oracle JDK offers "installers" (msi, rpm, deb, etc.) which not only place the JDK binaries in your system but also contain update rules and in some cases handle some common configurations like set common environmental variables (such as, JAVA_HOME in Windows) and establish file associations (such as, use java to launch .jar files). OpenJDK is offered only as compressed archive (tar.gz or .zip). javac —release for release values 9 and 10 behave differently. Oracle JDK binaries include APIs that were not added to OpenJDK binaries such as javafx, resource management, and (pre JDK 11 changes) JFR APIs. Usage Logging is only available in Oracle JDK. Oracle JDK requires that third-party cryptographic providers be signed with a Java Cryptography Extension (JCE) Code Signing Certificate. OpenJDK continues allowing the use of unsigned third-party crypto providers. The output of java -version is different. Oracle JDK ret...

Spring Integration - Integration Pattern

Integration Pattern The IntegrationPattern abstraction has been introduced to indicate which enterprise integration pattern (an IntegrationPatternType) and category a Spring Integration component belongs to. See JavaDocs and Integration Graph . ReactiveMessageHandler The ReactiveMessageHandler is now natively supported in the framework. See ReactiveMessageHandler for more information.

Angular 9 Is Now Available - What new?

The 9.0.0 release of Angular is here! This release including the framework, Angular Material, and the CLI. This release switches applications to the Ivy compiler and runtime by default, and introduces improved ways of testing components. This is one of the biggest updates to Angular made in the past 3 years, it empowers developers to build better applications and contribute to the Angular ecosystem. How to update to version 9 First, update to the latest version of 8 ng update @angular/cli@8 @angular/core@8 Then, update to 9 ng update @angular/cli @angular/core Ivy Version 9 moves all applications to use the Ivy compiler and runtime by default. Ivy compiler and runtime advantages: Smaller bundle sizes Faster testing Better debugging Improved CSS class and style binding Improved type checking Improved build errors Improved build times, enabling AOT on by default Improved Internationalization Faster testing Previously, TestBed would recompile all compone...

Lowest common ancestor

Image
Write a program to find the least common ancestor? The lowest common ancestor (LCA) of two nodes v and w in a tree, where we define each node to be a descendant of itself (so if v has a direct connection from w, w is the lowest common ancestor). The LCA of v and w in T is the shared ancestor of v and w that is located farthest from the root. Computation of lowest common ancestors may be useful, for instance, as part of a procedure for determining the distance between pairs of nodes in a tree: the distance from v to w can be computed as the distance from the root to v, plus the distance from the root to w, minus twice the distance from the root to their lowest common ancestor (Djidjev, Pantziou & Zaroliagis 1991). In ontologies, the lowest common ancestor is also known as the least common ancestor. // This function returns pointer to LCA of two given      // values n1 and n2.      // v1 is set as true by this function if n1 is found  ...

Spring Elasticsearch Repositories

Elasticsearch Repositories Query creation Generally the query creation mechanism for Elasticsearch works as described in Query methods. Here’s a short example of what a Elasticsearch query method translates into: Query creation from method names interface BookRepository extends Repository<Book, String> {   List<Book> findByNameAndPrice(String name, Integer price); } The method name above will be translated into the following Elasticsearch json query { "bool" :     { "must" :         [             { "field" : {"name" : "?"} },             { "field" : {"price" : "?"} }         ]     } } Using @Query Annotation Declare query at the method using the @Query annotation. interface BookRepository extends ElasticsearchRepository<Book, String> {     @Query("{\"bool\" : {\"must\" : {\"field\" : {\"name\" : \"?0\"}}}}...

Spring Elasticsearch Operations

Elasticsearch Operations Spring Data Elasticsearch uses two interfaces to define the operations that can be called against an Elasticsearch index. These are ElasticsearchOperations and ReactiveElasticsearchOperations. Whereas the first is used with the classic synchronous implementations, the second one uses reactive infrastructure. The default implementations of the interfaces offer: Read/Write mapping support for domain types. A rich query and criteria api. Resource management and Exception translation. ElasticsearchTemplate The ElasticsearchTemplate is an implementation of the ElasticsearchOperations interface using the Transport Client. @Configuration public class TransportClientConfig extends ElasticsearchConfigurationSupport {   @Bean   public Client elasticsearchClient() throws UnknownHostException {                      Settings settings = Settings.builder().put("cluster.name", "elasticsearch")....

Spring Elasticsearch Object Mapping

Elasticsearch Object Mapping Spring Data Elasticsearch allows to choose between two mapping implementations abstracted via the EntityMapper interface: Jackson Object Mapping Meta Model Object Mapping Jackson Object Mapping The Jackson2 based approach (used by default) utilizes a customized ObjectMapper instance with spring data specific modules. Extensions to the actual mapping need to be customized via Jackson annotations like @JsonInclude. @Configuration public class Config extends AbstractElasticsearchConfiguration {    @Override   public RestHighLevelClient elasticsearchClient() {     return RestClients.create(ClientConfiguration.create("localhost:9200")).rest();   } } AbstractElasticsearchConfiguration already defines a Jackson2 based entityMapper via ElasticsearchConfigurationSupport. CustomConversions, @ReadingConverter & @WritingConverter cannot be applied when using the Jackson based EntityMapper. Setting the name of a...

Spring Elasticsearch Clients

Spring Elasticsearch Clients Spring data Elasticsearch operates upon an Elasticsearch client that is connected to a single Elasticsearch node or a cluster. Although the Elasticsearch Client can be used to work with the cluster, applications using Spring Data Elasticsearch normally use the higher level abstractions of Elasticsearch Operations and Elasticsearch Repositories. Transport Client static class Config {   @Bean   Client client() {   Settings settings = Settings.builder()     .put("cluster.name", "elasticsearch")          .build();   TransportClient client = new PreBuiltTransportClient(settings);     client.addTransportAddress(new TransportAddress(InetAddress.getByName("127.0.0.1")       , 9300));                                    return client;   } } // ... IndexRequest request = ...

Core Java HTTP Client

The HTTP Client was added in Java 11. It can be used to request HTTP resources over the network. It supports HTTP/1.1 and HTTP/2, both synchronous and asynchronous programming models, handles request and response bodies as reactive-streams. GET request that prints the response body as a String HttpClient client = HttpClient.newHttpClient(); HttpRequest request = HttpRequest.newBuilder()       .uri(URI.create("http://openjdk.java.net/"))       .build(); client.sendAsync(request, BodyHandlers.ofString())       .thenApply(HttpResponse::body)       .thenAccept(System.out::println)       .join(); HttpClient To send a request, first create an HttpClient from its builder. The preferred protocol version ( HTTP/1.1 or HTTP/2 ) Whether to follow redirects A proxy An authenticator HttpClient client = HttpClient.newBuilder()       .version(Version.HTTP_2)       .fo...