remove use of FileInputStream/FileOutputStream for gc concerns

patcb by dbrosius reviewed by rstupp for CASSANDRA-13452
This commit is contained in:
Dave Brosius 2017-04-17 07:33:48 -04:00
parent f3dfb15031
commit 74bdf633e9
10 changed files with 66 additions and 46 deletions

View File

@ -17,6 +17,8 @@
*/
package org.apache.cassandra.db.commitlog;
import java.nio.file.Files;
import java.nio.file.Path;
import java.io.*;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
@ -397,9 +399,9 @@ public class CommitLogReader
catch (Throwable t)
{
JVMStabilityInspector.inspectThrowable(t);
File f = File.createTempFile("mutation", "dat");
Path p = Files.createTempFile("mutation", "dat");
try (DataOutputStream out = new DataOutputStream(new FileOutputStream(f)))
try (DataOutputStream out = new DataOutputStream(Files.newOutputStream(p)))
{
out.write(inputBuffer, 0, size);
}
@ -409,7 +411,7 @@ public class CommitLogReader
String.format(
"Unexpected error deserializing mutation; saved to %s. " +
"This may be caused by replaying a mutation against a table with the same name but incompatible schema. " +
"Exception follows: %s", f.getAbsolutePath(), t),
"Exception follows: %s", p.toString(), t),
CommitLogReadErrorReason.MUTATION_ERROR,
false));
return;

View File

@ -17,6 +17,9 @@
*/
package org.apache.cassandra.gms;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.nio.file.Path;
import java.io.*;
import java.lang.management.ManagementFactory;
import java.net.InetAddress;
@ -215,15 +218,18 @@ public class FailureDetector implements IFailureDetector, FailureDetectorMBean
*/
public void dumpInterArrivalTimes()
{
File file = FileUtils.createTempFile("failuredetector-", ".dat");
Path path = null;
try {
path = Files.createTempFile("failuredetector-", ".dat");
try (OutputStream os = new BufferedOutputStream(new FileOutputStream(file, true)))
{
os.write(toString().getBytes());
try (OutputStream os = new BufferedOutputStream(Files.newOutputStream(path, StandardOpenOption.APPEND)))
{
os.write(toString().getBytes());
}
}
catch (IOException e)
{
throw new FSWriteError(e, file);
throw new FSWriteError(e, (path == null) ? null : path.toFile());
}
}

View File

@ -19,7 +19,9 @@ package org.apache.cassandra.hadoop.cql3;
* under the License.
*
*/
import java.io.FileInputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.io.InputStream;
import java.io.IOException;
import java.security.KeyManagementException;
import java.security.KeyStore;
@ -86,7 +88,7 @@ public class CqlConfigHelper
private static final String OUTPUT_CQL = "cassandra.output.cql";
private static final String OUTPUT_NATIVE_PORT = "cassandra.output.native.port";
/**
* Set the CQL columns for the input of this job.
*
@ -97,10 +99,10 @@ public class CqlConfigHelper
{
if (columns == null || columns.isEmpty())
return;
conf.set(INPUT_CQL_COLUMNS_CONFIG, columns);
}
/**
* Set the CQL query Limit for the input of this job.
*
@ -127,10 +129,10 @@ public class CqlConfigHelper
{
if (clauses == null || clauses.isEmpty())
return;
conf.set(INPUT_CQL_WHERE_CLAUSE_CONFIG, clauses);
}
/**
* Set the CQL prepared statement for the output of this job.
*
@ -141,7 +143,7 @@ public class CqlConfigHelper
{
if (cql == null || cql.isEmpty())
return;
conf.set(OUTPUT_CQL, cql);
}
@ -283,7 +285,7 @@ public class CqlConfigHelper
return conf.get(OUTPUT_CQL);
}
private static Optional<Integer> getProtocolVersion(Configuration conf)
private static Optional<Integer> getProtocolVersion(Configuration conf)
{
return getIntSetting(INPUT_NATIVE_PROTOCOL_VERSION, conf);
}
@ -331,7 +333,7 @@ public class CqlConfigHelper
if (sslOptions.isPresent())
builder.withSSL(sslOptions.get());
if (protocolVersion.isPresent())
if (protocolVersion.isPresent())
{
builder.withProtocolVersion(ProtocolVersion.fromInt(protocolVersion.get()));
}
@ -356,7 +358,7 @@ public class CqlConfigHelper
public static void setInputMaxSimultReqPerConnections(Configuration conf, String reqs)
{
conf.set(INPUT_NATIVE_MAX_SIMULT_REQ_PER_CONNECTION, reqs);
}
}
public static void setInputNativeConnectionTimeout(Configuration conf, String timeout)
{
@ -396,7 +398,7 @@ public class CqlConfigHelper
public static void setInputNativeSSLTruststorePath(Configuration conf, String path)
{
conf.set(INPUT_NATIVE_SSL_TRUST_STORE_PATH, path);
}
}
public static void setInputNativeSSLKeystorePath(Configuration conf, String path)
{
@ -452,7 +454,7 @@ public class CqlConfigHelper
}
return poolingOptions;
}
}
private static QueryOptions getReadQueryOptions(Configuration conf)
{
@ -476,7 +478,7 @@ public class CqlConfigHelper
Optional<Integer> sendBufferSize = getInputNativeSendBufferSize(conf);
Optional<Integer> soLinger = getInputNativeSolinger(conf);
Optional<Boolean> tcpNoDelay = getInputNativeTcpNodelay(conf);
Optional<Boolean> reuseAddress = getInputNativeReuseAddress(conf);
Optional<Boolean> reuseAddress = getInputNativeReuseAddress(conf);
Optional<Boolean> keepAlive = getInputNativeKeepAlive(conf);
if (connectTimeoutMillis.isPresent())
@ -494,7 +496,7 @@ public class CqlConfigHelper
if (reuseAddress.isPresent())
socketOptions.setReuseAddress(reuseAddress.get());
if (keepAlive.isPresent())
socketOptions.setKeepAlive(keepAlive.get());
socketOptions.setKeepAlive(keepAlive.get());
return socketOptions;
}
@ -565,7 +567,7 @@ public class CqlConfigHelper
String setting = conf.get(parameter);
if (setting == null)
return Optional.absent();
return Optional.of(Integer.valueOf(setting));
return Optional.of(Integer.valueOf(setting));
}
private static Optional<Boolean> getBooleanSetting(String parameter, Configuration conf)
@ -573,7 +575,7 @@ public class CqlConfigHelper
String setting = conf.get(parameter);
if (setting == null)
return Optional.absent();
return Optional.of(Boolean.valueOf(setting));
return Optional.of(Boolean.valueOf(setting));
}
private static Optional<String> getStringSetting(String parameter, Configuration conf)
@ -581,7 +583,7 @@ public class CqlConfigHelper
String setting = conf.get(parameter);
if (setting == null)
return Optional.absent();
return Optional.of(setting);
return Optional.of(setting);
}
private static AuthProvider getClientAuthProvider(String factoryClassName, Configuration conf)
@ -623,7 +625,7 @@ public class CqlConfigHelper
TrustManagerFactory tmf = null;
if (truststorePath.isPresent())
{
try (FileInputStream tsf = new FileInputStream(truststorePath.get()))
try (InputStream tsf = Files.newInputStream(Paths.get(truststorePath.get())))
{
KeyStore ts = KeyStore.getInstance("JKS");
ts.load(tsf, truststorePassword.isPresent() ? truststorePassword.get().toCharArray() : null);
@ -635,7 +637,7 @@ public class CqlConfigHelper
KeyManagerFactory kmf = null;
if (keystorePath.isPresent())
{
try (FileInputStream ksf = new FileInputStream(keystorePath.get()))
try (InputStream ksf = Files.newInputStream(Paths.get(keystorePath.get())))
{
KeyStore ks = KeyStore.getInstance("JKS");
ks.load(ksf, keystorePassword.isPresent() ? keystorePassword.get().toCharArray() : null);

View File

@ -17,6 +17,8 @@
*/
package org.apache.cassandra.io.compress;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.io.BufferedOutputStream;
import java.io.DataInput;
import java.io.DataInputStream;
@ -103,7 +105,7 @@ public class CompressionMetadata
{
this.indexFilePath = indexFilePath;
try (DataInputStream stream = new DataInputStream(new FileInputStream(indexFilePath)))
try (DataInputStream stream = new DataInputStream(Files.newInputStream(Paths.get(indexFilePath))))
{
String compressorName = stream.readUTF();
int optionCount = stream.readInt();

View File

@ -17,6 +17,8 @@
*/
package org.apache.cassandra.io.sstable.format;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.io.*;
import java.lang.ref.WeakReference;
import java.nio.ByteBuffer;
@ -730,7 +732,7 @@ public abstract class SSTableReader extends SSTable implements SelfRefCounted<SS
*/
private void loadBloomFilter() throws IOException
{
try (DataInputStream stream = new DataInputStream(new BufferedInputStream(new FileInputStream(descriptor.filenameFor(Component.FILTER)))))
try (DataInputStream stream = new DataInputStream(new BufferedInputStream(Files.newInputStream(Paths.get(descriptor.filenameFor(Component.FILTER))))))
{
bf = FilterFactory.deserialize(stream, true);
}
@ -871,7 +873,7 @@ public abstract class SSTableReader extends SSTable implements SelfRefCounted<SS
try
{
TableMetadata metadata = metadata();
iStream = new DataInputStream(new FileInputStream(summariesFile));
iStream = new DataInputStream(Files.newInputStream(summariesFile.toPath()));
indexSummary = IndexSummary.serializer.deserialize(
iStream, getPartitioner(),
metadata.params.minIndexInterval, metadata.params.maxIndexInterval);

View File

@ -17,7 +17,9 @@
*/
package org.apache.cassandra.security;
import java.io.FileInputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.io.InputStream;
import java.io.IOException;
import java.security.Key;
import java.security.KeyStore;
@ -46,7 +48,7 @@ public class JKSKeyProvider implements KeyProvider
{
this.options = options;
logger.info("initializing keystore from file {}", options.get(PROP_KEYSTORE));
try (FileInputStream inputStream = new FileInputStream(options.get(PROP_KEYSTORE)))
try (InputStream inputStream = Files.newInputStream(Paths.get(options.get(PROP_KEYSTORE))))
{
store = KeyStore.getInstance(options.get(PROP_KEYSTORE_TYPE));
store.load(inputStream, options.get(PROP_KEYSTORE_PW).toCharArray());

View File

@ -17,8 +17,9 @@
*/
package org.apache.cassandra.security;
import java.io.FileInputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.io.InputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
@ -155,8 +156,8 @@ public final class SSLFactory
@SuppressWarnings("resource")
public static SSLContext createSSLContext(EncryptionOptions options, boolean buildTruststore) throws IOException
{
FileInputStream tsf = null;
FileInputStream ksf = null;
InputStream tsf = null;
InputStream ksf = null;
SSLContext ctx;
try
{
@ -165,7 +166,7 @@ public final class SSLFactory
if(buildTruststore)
{
tsf = new FileInputStream(options.truststore);
tsf = Files.newInputStream(Paths.get(options.truststore));
TrustManagerFactory tmf = TrustManagerFactory.getInstance(options.algorithm);
KeyStore ts = KeyStore.getInstance(options.store_type);
ts.load(tsf, options.truststore_password.toCharArray());
@ -173,7 +174,7 @@ public final class SSLFactory
trustManagers = tmf.getTrustManagers();
}
ksf = new FileInputStream(options.keystore);
ksf = Files.newInputStream(Paths.get((options.keystore)));
KeyManagerFactory kmf = KeyManagerFactory.getInstance(options.algorithm);
KeyStore ks = KeyStore.getInstance(options.store_type);
ks.load(ksf, options.keystore_password.toCharArray());

View File

@ -19,6 +19,7 @@ package org.apache.cassandra.tools;
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.List;
@ -214,7 +215,7 @@ public class SSTableMetadataViewer
if (!summariesFile.exists())
return;
try (DataInputStream iStream = new DataInputStream(new FileInputStream(summariesFile)))
try (DataInputStream iStream = new DataInputStream(Files.newInputStream(summariesFile.toPath())))
{
Pair<DecoratedKey, DecoratedKey> firstLast = new IndexSummary.IndexSummarySerializer().deserializeFirstLastKey(iStream, partitioner);
out.printf("First token: %s (key=%s)%n", firstLast.left.getToken(), keyType.getString(firstLast.left.getKey()));

View File

@ -148,7 +148,7 @@ public class StressGraph
currentThreadCount = tc.group(2);
}
}
// Detect mode changes
if (line.equals(StressMetrics.HEAD))
{
@ -232,7 +232,7 @@ public class StressGraph
private JSONObject createJSONStats(JSONObject json)
{
try (InputStream logStream = new FileInputStream(stressSettings.graph.temporaryLogFile))
try (InputStream logStream = Files.newInputStream(stressSettings.graph.temporaryLogFile.toPath()))
{
JSONArray stats;
if (json == null)

View File

@ -1,6 +1,6 @@
package org.apache.cassandra.stress.settings;
/*
*
*
* 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
@ -8,19 +8,21 @@ package org.apache.cassandra.stress.settings;
* 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.
*
*
*/
import java.nio.file.Files;
import java.nio.file.Paths;
import java.io.*;
import java.net.InetAddress;
import java.net.InetSocketAddress;
@ -44,7 +46,7 @@ public class SettingsNode implements Serializable
{
String node;
List<String> tmpNodes = new ArrayList<>();
try (BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(options.file.value()))))
try (BufferedReader in = new BufferedReader(new InputStreamReader(Files.newInputStream(Paths.get(options.file.value())))))
{
while ((node = in.readLine()) != null)
{