8153955: increase java.util.logging.FileHandler MAX_LOCKS limit
Summary: This patch adds a new configurable property "java.util.logging.FileHandler.maxLocks" to java.util.logging.FileHandler which can be defined in the logging configuration file and makes it possible to configure the maximum number of concurrent log file locks a FileHandler can handle. If not overridden, the default value of maxLocks (100) remains unchanged.
Reviewed-by: dfuchs, coffeys
--- a/jdk/src/java.logging/share/classes/java/util/logging/FileHandler.java Tue Jun 28 14:39:09 2016 +0800
+++ b/jdk/src/java.logging/share/classes/java/util/logging/FileHandler.java Mon Jun 27 11:52:49 2016 +0530
@@ -94,6 +94,9 @@
* <li> <handler-name>.append
* specifies whether the FileHandler should append onto
* any existing files (defaults to false). </li>
+ * <li> <handler-name>.maxLocks
+ * specifies the maximum number of concurrent locks held by
+ * FileHandler (defaults to 100). </li>
* </ul>
* <p>
* For example, the properties for {@code FileHandler} would be:
@@ -157,6 +160,7 @@
private FileChannel lockFileChannel;
private File files[];
private static final int MAX_LOCKS = 100;
+ private int maxLocks = MAX_LOCKS;
private static final Set<String> locks = new HashSet<>();
/**
@@ -235,6 +239,12 @@
setLevel(manager.getLevelProperty(cname + ".level", Level.ALL));
setFilter(manager.getFilterProperty(cname + ".filter", null));
setFormatter(manager.getFormatterProperty(cname + ".formatter", new XMLFormatter()));
+ // Initialize maxLocks from the logging.properties file.
+ // If invalid/no property is provided 100 will be used as a default value.
+ maxLocks = manager.getIntProperty(cname + ".maxLocks", MAX_LOCKS);
+ if(maxLocks <= 0) {
+ maxLocks = MAX_LOCKS;
+ }
try {
setEncoding(manager.getStringProperty(cname +".encoding", null));
} catch (Exception ex) {
@@ -476,7 +486,7 @@
int unique = -1;
for (;;) {
unique++;
- if (unique > MAX_LOCKS) {
+ if (unique > maxLocks) {
throw new IOException("Couldn't get lock for " + pattern);
}
// Generate a lock file name from the "unique" int.
--- a/jdk/src/java.logging/share/conf/logging.properties Tue Jun 28 14:39:09 2016 +0800
+++ b/jdk/src/java.logging/share/conf/logging.properties Mon Jun 27 11:52:49 2016 +0530
@@ -37,6 +37,10 @@
java.util.logging.FileHandler.pattern = %h/java%u.log
java.util.logging.FileHandler.limit = 50000
java.util.logging.FileHandler.count = 1
+# Default number of locks FileHandler can obtain synchronously.
+# This specifies maximum number of attempts to obtain lock file by FileHandler
+# implemented by incrementing the unique field %u as per FileHandler API documentation.
+java.util.logging.FileHandler.maxLocks = 100
java.util.logging.FileHandler.formatter = java.util.logging.XMLFormatter
# Limit the message that are printed on the console to INFO and above.
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/util/logging/FileHandlerMaxLocksTest.java Mon Jun 27 11:52:49 2016 +0530
@@ -0,0 +1,114 @@
+/*
+ * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 8153955
+ * @summary test the FileHandler's new property
+ * "java.util.logging.FileHandler.maxLocks" which will be present in
+ * "logging.properties" file with default value of 100. This property can be
+ * overriden by specifying this property in the custom config file.
+ * @library /lib/testlibrary
+ * @build jdk.testlibrary.FileUtils
+ * @author rpatil
+ * @run main/othervm FileHandlerMaxLocksTest
+ */
+import java.io.File;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.logging.FileHandler;
+import jdk.testlibrary.FileUtils;
+
+public class FileHandlerMaxLocksTest {
+
+ private static final String LOGGER_DIR = "logger-dir";
+ private static final String MAX_LOCK_PROPERTY = "java.util.logging.FileHandler.maxLocks = 200";
+ private static final String CONFIG_FILE_NAME = "logging.properties";
+
+ public static void main(String[] args) throws Exception {
+ File loggerDir = createLoggerDir();
+ String configFilePath = loggerDir.getPath() + File.separator + CONFIG_FILE_NAME;
+ File configFile = new File(configFilePath);
+ createFile(configFile, false);
+ System.setProperty("java.util.logging.config.file", configFilePath);
+ List<FileHandler> fileHandlers = new ArrayList<>();
+ try (FileWriter writer = new FileWriter(configFile)) {
+ writer.write(MAX_LOCK_PROPERTY);
+ writer.flush();
+ // 200 raises the default limit of 100, we try 102 times
+ for (int i = 0; i < 102; i++) {
+ fileHandlers.add(new FileHandler(loggerDir.getPath() + File.separator + "test_%u.log"));
+ }
+ } catch (IOException ie) {
+ throw new RuntimeException("Test Failed: " + ie.getMessage());
+ } finally {
+ for (FileHandler fh : fileHandlers) {
+ fh.close();
+ }
+ FileUtils.deleteFileTreeWithRetry(Paths.get(loggerDir.getPath()));
+ }
+ }
+
+ /**
+ * Create a writable directory in user directory for the test
+ *
+ * @return writable directory created that needs to be deleted when done
+ * @throws RuntimeException
+ */
+ private static File createLoggerDir() throws RuntimeException {
+ String userDir = System.getProperty("user.dir", ".");
+ File loggerDir = new File(userDir, LOGGER_DIR);
+ if (!createFile(loggerDir, true)) {
+ throw new RuntimeException("Test failed: unable to create"
+ + " writable working directory "
+ + loggerDir.getAbsolutePath());
+ }
+ // System.out.println("Created Logger Directory: " + loggerDir.getPath());
+ return loggerDir;
+ }
+
+ /**
+ * @param newFile File to be created
+ * @param makeDirectory is File to be created is directory
+ * @return true if file already exists or creation succeeded
+ */
+ private static boolean createFile(File newFile, boolean makeDirectory) {
+ if (newFile.exists()) {
+ return true;
+ }
+ if (makeDirectory) {
+ return newFile.mkdir();
+ } else {
+ try {
+ return newFile.createNewFile();
+ } catch (IOException ie) {
+ System.err.println("Not able to create file: " + newFile
+ + ", IOException: " + ie.getMessage());
+ return false;
+ }
+ }
+ }
+}