Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CASSANDRA-16926] Phase 1: Mockable Filesystem #17

Open
wants to merge 1 commit into
base: CASSANDRA-16926-trunk-base
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
4 changes: 2 additions & 2 deletions checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@
</module>
<module name="IllegalImport">
<property name="illegalPkgs" value=""/>
<property name="illegalClasses" value="java.util.concurrent.Semaphore,java.util.concurrent.CountDownLatch,java.util.concurrent.Executors,java.util.concurrent.LinkedBlockingQueue,java.util.concurrent.SynchronousQueue,java.util.concurrent.ArrayBlockingQueue,com.google.common.util.concurrent.Futures"/>
<property name="illegalClasses" value="java.io.File,java.io.FileInputStream,java.io.FileOutputStream,java.io.FileReader,java.io.FileWriter,java.io.RandomAccessFile,java.util.concurrent.Semaphore,java.util.concurrent.CountDownLatch,java.util.concurrent.Executors,java.util.concurrent.LinkedBlockingQueue,java.util.concurrent.SynchronousQueue,java.util.concurrent.ArrayBlockingQueue,com.google.common.util.concurrent.Futures,java.util.concurrent.CompletableFuture"/>
</module>
<module name="IllegalInstantiation">
<property name="classes" value="java.lang.Thread,java.util.concurrent.FutureTask,java.util.concurrent.Semaphore,java.util.concurrent.CountDownLatch,java.util.concurrent.ScheduledThreadPoolExecutor,java.util.concurrent.ThreadPoolExecutor,java.util.concurrent.ForkJoinPool))"/>
<property name="classes" value="java.io.File,java.lang.Thread,java.util.concurrent.FutureTask,java.util.concurrent.Semaphore,java.util.concurrent.CountDownLatch,java.util.concurrent.ScheduledThreadPoolExecutor,java.util.concurrent.ThreadPoolExecutor,java.util.concurrent.ForkJoinPool))"/>
</module>
</module>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@
public enum AuditLogEntryCategory
{
QUERY, DML, DDL, DCL, OTHER, AUTH, ERROR, PREPARE
}
}
33 changes: 17 additions & 16 deletions src/java/org/apache/cassandra/cache/AutoSavingCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@
package org.apache.cassandra.cache;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.NoSuchFileException;
import java.util.*;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

import org.apache.cassandra.io.util.File;
import org.cliffc.high_scale_lib.NonBlockingHashSet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -59,7 +60,7 @@ public class AutoSavingCache<K extends CacheKey, V> extends InstrumentingCache<K
public interface IStreamFactory
{
InputStream getInputStream(File dataPath, File crcPath) throws IOException;
OutputStream getOutputStream(File dataPath, File crcPath) throws FileNotFoundException;
OutputStream getOutputStream(File dataPath, File crcPath);
}

private static final Logger logger = LoggerFactory.getLogger(AutoSavingCache.class);
Expand Down Expand Up @@ -251,12 +252,12 @@ public int loadSaved()
catch (CorruptFileException e)
{
JVMStabilityInspector.inspectThrowable(e);
logger.warn(String.format("Non-fatal checksum error reading saved cache %s", dataPath.getAbsolutePath()), e);
logger.warn(String.format("Non-fatal checksum error reading saved cache %s", dataPath.absolutePath()), e);
}
catch (Throwable t)
{
JVMStabilityInspector.inspectThrowable(t);
logger.info(String.format("Harmless error reading saved cache %s", dataPath.getAbsolutePath()), t);
logger.info(String.format("Harmless error reading saved cache %s", dataPath.absolutePath()), t);
}
finally
{
Expand Down Expand Up @@ -369,7 +370,7 @@ public void saveCache()
break;
}
}
catch (FileNotFoundException e)
catch (FileNotFoundException | NoSuchFileException e)
{
throw new RuntimeException(e);
}
Expand All @@ -381,13 +382,13 @@ public void saveCache()
File cacheFile = getCacheDataPath(CURRENT_VERSION);
File crcFile = getCacheCrcPath(CURRENT_VERSION);

cacheFile.delete(); // ignore error if it didn't exist
crcFile.delete();
cacheFile.tryDelete(); // ignore error if it didn't exist
crcFile.tryDelete();

if (!cacheFilePaths.left.renameTo(cacheFile))
if (!cacheFilePaths.left.tryMove(cacheFile))
logger.error("Unable to rename {} to {}", cacheFilePaths.left, cacheFile);

if (!cacheFilePaths.right.renameTo(crcFile))
if (!cacheFilePaths.right.tryMove(crcFile))
logger.error("Unable to rename {} to {}", cacheFilePaths.right, crcFile);

logger.info("Saved {} ({} items) in {} ms", cacheType, keysWritten, TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start));
Expand All @@ -397,15 +398,15 @@ private Pair<File, File> tempCacheFiles()
{
File dataPath = getCacheDataPath(CURRENT_VERSION);
File crcPath = getCacheCrcPath(CURRENT_VERSION);
return Pair.create(FileUtils.createTempFile(dataPath.getName(), null, dataPath.getParentFile()),
FileUtils.createTempFile(crcPath.getName(), null, crcPath.getParentFile()));
return Pair.create(FileUtils.createTempFile(dataPath.name(), null, dataPath.parent()),
FileUtils.createTempFile(crcPath.name(), null, crcPath.parent()));
}

private void deleteOldCacheFiles()
{
File savedCachesDir = new File(DatabaseDescriptor.getSavedCachesLocation());
assert savedCachesDir.exists() && savedCachesDir.isDirectory();
File[] files = savedCachesDir.listFiles();
File[] files = savedCachesDir.tryList();
if (files != null)
{
String cacheNameFormat = String.format("%s-%s.db", cacheType.toString(), CURRENT_VERSION);
Expand All @@ -414,11 +415,11 @@ private void deleteOldCacheFiles()
if (!file.isFile())
continue; // someone's been messing with our directory. naughty!

if (file.getName().endsWith(cacheNameFormat)
|| file.getName().endsWith(cacheType.toString()))
if (file.name().endsWith(cacheNameFormat)
|| file.name().endsWith(cacheType.toString()))
{
if (!file.delete())
logger.warn("Failed to delete {}", file.getAbsolutePath());
if (!file.tryDelete())
logger.warn("Failed to delete {}", file.absolutePath());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ public enum CassandraRelevantProperties
*/
LOG_DIR_AUDIT("cassandra.logdir.audit"),

CONSISTENT_DIRECTORY_LISTINGS("cassandra.consistent_directory_listings", "false"),

//cassandra properties (without the "cassandra." prefix)

/**
Expand Down
101 changes: 22 additions & 79 deletions src/java/org/apache/cassandra/config/DatabaseDescriptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,9 @@
*/
package org.apache.cassandra.config;

import java.io.File;
import java.io.IOException;
import java.net.*;
import java.nio.file.FileStore;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
Expand All @@ -36,6 +32,7 @@
import com.google.common.primitives.Longs;
import com.google.common.util.concurrent.RateLimiter;

import org.apache.cassandra.io.util.File;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -59,6 +56,7 @@
import org.apache.cassandra.io.FSWriteError;
import org.apache.cassandra.io.util.DiskOptimizationStrategy;
import org.apache.cassandra.io.util.FileUtils;
import org.apache.cassandra.io.util.PathUtils;
import org.apache.cassandra.io.util.SpinningDiskOptimizationStrategy;
import org.apache.cassandra.io.util.SsdDiskOptimizationStrategy;
import org.apache.cassandra.locator.DynamicEndpointSnitch;
Expand Down Expand Up @@ -549,23 +547,13 @@ else if (conf.repair_session_space_in_mb > (int) (Runtime.getRuntime().maxMemory
if (conf.commitlog_total_space_in_mb == null)
{
final int preferredSizeInMB = 8192;
try
{
// use 1/4 of available space. See discussion on #10013 and #10199
final long totalSpaceInBytes = guessFileStore(conf.commitlog_directory).getTotalSpace();
conf.commitlog_total_space_in_mb = calculateDefaultSpaceInMB("commitlog",
conf.commitlog_directory,
"commitlog_total_space_in_mb",
preferredSizeInMB,
totalSpaceInBytes, 1, 4);

}
catch (IOException e)
{
logger.debug("Error checking disk space", e);
throw new ConfigurationException(String.format("Unable to check disk space available to '%s'. Perhaps the Cassandra user does not have the necessary permissions",
conf.commitlog_directory), e);
}
// use 1/4 of available space. See discussion on #10013 and #10199
final long totalSpaceInBytes = tryGetSpace(conf.commitlog_directory, FileStore::getTotalSpace);
conf.commitlog_total_space_in_mb = calculateDefaultSpaceInMB("commitlog",
conf.commitlog_directory,
"commitlog_total_space_in_mb",
preferredSizeInMB,
totalSpaceInBytes, 1, 4);
}

if (conf.cdc_enabled)
Expand All @@ -582,22 +570,13 @@ else if (conf.repair_session_space_in_mb > (int) (Runtime.getRuntime().maxMemory
if (conf.cdc_total_space_in_mb == 0)
{
final int preferredSizeInMB = 4096;
try
{
// use 1/8th of available space. See discussion on #10013 and #10199 on the CL, taking half that for CDC
final long totalSpaceInBytes = guessFileStore(conf.cdc_raw_directory).getTotalSpace();
conf.cdc_total_space_in_mb = calculateDefaultSpaceInMB("cdc",
conf.cdc_raw_directory,
"cdc_total_space_in_mb",
preferredSizeInMB,
totalSpaceInBytes, 1, 8);
}
catch (IOException e)
{
logger.debug("Error checking disk space", e);
throw new ConfigurationException(String.format("Unable to check disk space available to '%s'. Perhaps the Cassandra user does not have the necessary permissions",
conf.cdc_raw_directory), e);
}
// use 1/8th of available space. See discussion on #10013 and #10199 on the CL, taking half that for CDC
final long totalSpaceInBytes = tryGetSpace(conf.cdc_raw_directory, FileStore::getTotalSpace);
conf.cdc_total_space_in_mb = calculateDefaultSpaceInMB("cdc",
conf.cdc_raw_directory,
"cdc_total_space_in_mb",
preferredSizeInMB,
totalSpaceInBytes, 1, 8);
}

logger.info("cdc_enabled is true. Starting casssandra node with Change-Data-Capture enabled.");
Expand All @@ -609,7 +588,7 @@ else if (conf.repair_session_space_in_mb > (int) (Runtime.getRuntime().maxMemory
}
if (conf.data_file_directories == null || conf.data_file_directories.length == 0)
{
conf.data_file_directories = new String[]{ storagedir("data_file_directories") + File.separator + "data" };
conf.data_file_directories = new String[]{ storagedir("data_file_directories") + File.pathSeparator() + "data" };
}

long dataFreeBytes = 0;
Expand All @@ -627,7 +606,7 @@ else if (conf.repair_session_space_in_mb > (int) (Runtime.getRuntime().maxMemory
if (datadir.equals(conf.saved_caches_directory))
throw new ConfigurationException("saved_caches_directory must not be the same as any data_file_directories", false);

dataFreeBytes = saturatedSum(dataFreeBytes, getUnallocatedSpace(datadir));
dataFreeBytes = saturatedSum(dataFreeBytes, tryGetSpace(datadir, FileStore::getUnallocatedSpace));
}
if (dataFreeBytes < 64 * ONE_GB) // 64 GB
logger.warn("Only {} free across all data volumes. Consider adding more capacity to your cluster or removing obsolete snapshots",
Expand All @@ -642,7 +621,7 @@ else if (conf.repair_session_space_in_mb > (int) (Runtime.getRuntime().maxMemory
if (conf.local_system_data_file_directory.equals(conf.hints_directory))
throw new ConfigurationException("local_system_data_file_directory must not be the same as the hints_directory", false);

long freeBytes = getUnallocatedSpace(conf.local_system_data_file_directory);
long freeBytes = tryGetSpace(conf.local_system_data_file_directory, FileStore::getUnallocatedSpace);

if (freeBytes < ONE_GB)
logger.warn("Only {} free in the system data volume. Consider adding more capacity or removing obsolete snapshots",
Expand Down Expand Up @@ -871,7 +850,7 @@ static void applyRepairCommandPoolSize(Config config)

private static String storagedirFor(String type)
{
return storagedir(type + "_directory") + File.separator + type;
return storagedir(type + "_directory") + File.pathSeparator() + type;
}

private static String storagedir(String errMsgType)
Expand Down Expand Up @@ -1191,45 +1170,9 @@ private static long saturatedSum(long left, long right)
return sum < 0 ? Long.MAX_VALUE : sum;
}

private static FileStore guessFileStore(String dir) throws IOException
private static long tryGetSpace(String dir, PathUtils.IOToLongFunction<FileStore> getSpace)
{
Path path = Paths.get(dir);
while (true)
{
try
{
return FileUtils.getFileStore(path);
}
catch (IOException e)
{
if (e instanceof NoSuchFileException)
{
path = path.getParent();
if (path == null)
{
throw new ConfigurationException("Unable to find filesystem for '" + dir + "'.");
}
}
else
{
throw e;
}
}
}
}

private static long getUnallocatedSpace(String directory)
{
try
{
return guessFileStore(directory).getUnallocatedSpace();
}
catch (IOException e)
{
logger.debug("Error checking disk space", e);
throw new ConfigurationException(String.format("Unable to check disk space available to %s. Perhaps the Cassandra user does not have the necessary permissions",
directory), e);
}
return PathUtils.tryGetSpace(new File(dir).toPath(), getSpace, e -> { throw new ConfigurationException("Unable check disk space in '" + dir + "'. Perhaps the Cassandra user does not have the necessary permissions"); });
}

public static IEndpointSnitch createEndpointSnitch(boolean dynamic, String snitchClassName) throws ConfigurationException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
*/
package org.apache.cassandra.config;

import java.io.File;
import java.util.List;
import java.util.Objects;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList;

import org.apache.cassandra.io.util.File;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package org.apache.cassandra.config;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.Annotation;
Expand All @@ -35,6 +34,7 @@
import com.google.common.collect.Sets;
import com.google.common.io.ByteStreams;

import org.apache.cassandra.io.util.File;
import org.apache.commons.lang3.SystemUtils;

import org.slf4j.Logger;
Expand Down Expand Up @@ -79,14 +79,14 @@ private static URL getStorageConfigURL() throws ConfigurationException
url = loader.getResource(configUrl);
if (url == null)
{
String required = "file:" + File.separator + File.separator;
String required = "file:" + File.pathSeparator() + File.pathSeparator();
if (!configUrl.startsWith(required))
throw new ConfigurationException(String.format(
"Expecting URI in variable: [cassandra.config]. Found[%s]. Please prefix the file with [%s%s] for local " +
"files and [%s<server>%s] for remote files. If you are executing this from an external tool, it needs " +
"to set Config.setClientMode(true) to avoid loading configuration.",
configUrl, required, File.separator, required, File.separator));
throw new ConfigurationException("Cannot locate " + configUrl + ". If this is a local file, please confirm you've provided " + required + File.separator + " as a URI prefix.");
configUrl, required, File.pathSeparator(), required, File.pathSeparator()));
throw new ConfigurationException("Cannot locate " + configUrl + ". If this is a local file, please confirm you've provided " + required + File.pathSeparator() + " as a URI prefix.");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -861,4 +861,4 @@ public String toString()
return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,4 @@ private List<ByteBuffer> getOutputRow()
addSize(row);
return row;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,4 @@ public boolean allowUseOfSecondaryIndices()
{
return false;
}
}
}
Loading