Skip to content

Commit

Permalink
Moving format() to MutableJournal interface and removing root folder …
Browse files Browse the repository at this point in the history
…weirdness.
  • Loading branch information
jsimsa committed Mar 20, 2017
1 parent 8da1b9f commit f0f9ccf
Show file tree
Hide file tree
Showing 23 changed files with 197 additions and 165 deletions.
16 changes: 5 additions & 11 deletions core/server/common/src/main/java/alluxio/cli/Format.java
Expand Up @@ -17,6 +17,7 @@
import alluxio.RuntimeConstants; import alluxio.RuntimeConstants;
import alluxio.ServerUtils; import alluxio.ServerUtils;
import alluxio.master.journal.Journal; import alluxio.master.journal.Journal;
import alluxio.master.journal.MutableJournal;
import alluxio.underfs.UnderFileStatus; import alluxio.underfs.UnderFileStatus;
import alluxio.underfs.UnderFileSystem; import alluxio.underfs.UnderFileSystem;
import alluxio.underfs.options.DeleteOptions; import alluxio.underfs.options.DeleteOptions;
Expand Down Expand Up @@ -94,21 +95,14 @@ public static void main(String[] args) {
public static void format(String mode) throws IOException { public static void format(String mode) throws IOException {
if ("MASTER".equalsIgnoreCase(mode)) { if ("MASTER".equalsIgnoreCase(mode)) {
String masterJournal = Configuration.get(PropertyKey.MASTER_JOURNAL_FOLDER); String masterJournal = Configuration.get(PropertyKey.MASTER_JOURNAL_FOLDER);
Journal.Factory factory; MutableJournal.Factory factory;
try { try {
factory = new Journal.Factory(new URI(masterJournal)); factory = new MutableJournal.Factory(new URI(masterJournal));
} catch (URISyntaxException e) { } catch (URISyntaxException e) {
throw new IOException(e.getMessage()); throw new IOException(e.getMessage());
} }
// This is a small hack to format the root journal folder.
if (!factory.create("").format()) {
throw new RuntimeException("Failed to format root journal folder");
}
for (String masterServiceName : ServerUtils.getMasterServiceNames()) { for (String masterServiceName : ServerUtils.getMasterServiceNames()) {
if (!factory.create(masterServiceName).format()) { factory.create(masterServiceName).format();
throw new RuntimeException(
String.format("Failed to format %s journal folder", masterServiceName));
}
} }
} else if ("WORKER".equalsIgnoreCase(mode)) { } else if ("WORKER".equalsIgnoreCase(mode)) {
String workerDataFolder = Configuration.get(PropertyKey.WORKER_DATA_FOLDER); String workerDataFolder = Configuration.get(PropertyKey.WORKER_DATA_FOLDER);
Expand All @@ -135,5 +129,5 @@ public static void format(String mode) throws IOException {
} }
} }


private Format() {} // prevent instantiation private Format() {} // prevent instantiation
} }
Expand Up @@ -214,7 +214,7 @@ public void stop() throws IOException {


@Override @Override
public void transitionToLeader() { public void transitionToLeader() {
mJournal = Journal.Factory.create(mJournal.getLocation(), true); mJournal = MutableJournal.Factory.create(mJournal.getLocation());
} }


/** /**
Expand Down
Expand Up @@ -11,7 +11,7 @@


package alluxio.master; package alluxio.master;


import alluxio.master.journal.Journal; import alluxio.master.journal.JournalFactory;


import java.util.List; import java.util.List;


Expand All @@ -36,5 +36,5 @@ public interface MasterFactory {
* *
* @return a new {@link Master} instance or null if failed to launch the master * @return a new {@link Master} instance or null if failed to launch the master
*/ */
Master create(List<? extends Master> masters, Journal.Factory factory); Master create(List<? extends Master> masters, JournalFactory factory);
} }
Expand Up @@ -12,7 +12,6 @@
package alluxio.master.journal; package alluxio.master.journal;


import alluxio.master.journal.ufs.UfsJournal; import alluxio.master.journal.ufs.UfsJournal;
import alluxio.master.journal.ufs.UfsMutableJournal;
import alluxio.util.URIUtils; import alluxio.util.URIUtils;


import java.io.IOException; import java.io.IOException;
Expand All @@ -31,45 +30,23 @@ public interface Journal {
* A {@link Journal} factory. * A {@link Journal} factory.
*/ */
@ThreadSafe @ThreadSafe
final class Factory { final class Factory implements JournalFactory {
private final URI mBase; private final URI mBase;
private final boolean mMutable;


/** /**
* Creates a read-only journal factory with the specified base location. When journals are * Creates a read-only journal factory with the specified base location. When journals are
* created, their names are appended to the base path. * created, their names are appended to the base location.
* *
* @param base the base location for journals created by this factory * @param base the base location for journals created by this factory
*/ */
public Factory(URI base) { public Factory(URI base) {
this(base, false);
}

/**
* Creates a journal factory with the specified base location. When journals are
* created, their names are appended to the base path.
*
* @param base the base location for journals created by this factory
* @param mutable whether the journal is mutable or not
*/
public Factory(URI base, boolean mutable) {
mBase = base; mBase = base;
mMutable = mutable;
} }


/** @Override
* Creates a new journal using the given name.
*
* @param name the journal name
* @return a new instance of {@link Journal}
*/
public Journal create(String name) { public Journal create(String name) {
try { try {
if (!mMutable) { return new UfsJournal(URIUtils.appendPath(mBase, name));
return new UfsJournal(URIUtils.appendPath(mBase, name));
} else {
return new UfsMutableJournal(URIUtils.appendPath(mBase, name));
}
} catch (URISyntaxException e) { } catch (URISyntaxException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
Expand All @@ -82,33 +59,10 @@ public Journal create(String name) {
* @return a new instance of {@link Journal} * @return a new instance of {@link Journal}
*/ */
public static Journal create(URI location) { public static Journal create(URI location) {
return create(location, false); return new UfsJournal(location);
}

/**
* Creates a new journal using the given journal location.
*
* @param location the journal location
* @oaram mutable whether the journal is mutable or not
* @return a new instance of {@link Journal}
*/
public static Journal create(URI location, boolean mutable) {
if (!mutable) {
return new UfsJournal(location);
} else {
return new UfsMutableJournal(location);
}
} }
} }


/**
* Formats the journal.
*
* @return whether the format operation succeeded or not
* @throws IOException if an I/O error occurs
*/
boolean format() throws IOException;

/** /**
* @return the journal location * @return the journal location
*/ */
Expand Down
@@ -0,0 +1,14 @@
package alluxio.master.journal;

/**
* Factory for creating named journals.
*/
public interface JournalFactory {
/**
* Creates a new journal using the given name.
*
* @param name the journal name
* @return a new instance of {@link Journal}
*/
Journal create(String name);
}
Expand Up @@ -11,6 +11,13 @@


package alluxio.master.journal; package alluxio.master.journal;


import alluxio.master.journal.ufs.UfsMutableJournal;
import alluxio.util.URIUtils;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import javax.annotation.concurrent.ThreadSafe; import javax.annotation.concurrent.ThreadSafe;


/** /**
Expand All @@ -19,6 +26,51 @@
@ThreadSafe @ThreadSafe
public interface MutableJournal extends Journal { public interface MutableJournal extends Journal {


/**
* A {@link MutableJournal} factory.
*/
@ThreadSafe
final class Factory implements JournalFactory {
private final URI mBase;

/**
* Creates a read-write journal factory with the specified base location. When journals are
* created, their names are appended to the base path.
*
* @param base the base location for journals created by this factory
*/
public Factory(URI base) {
mBase = base;
}

@Override
public MutableJournal create(String name) {
try {
return new UfsMutableJournal(URIUtils.appendPath(mBase, name));
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}

/**
* Creates a new read-write journal using the given location.
*
* @param location the journal location
* @return a new instance of {@link Journal}
*/
public static MutableJournal create(URI location) {
return new UfsMutableJournal(location);
}
}

/**
* Formats the journal.
*
* @return whether the format operation succeeded or not
* @throws IOException if an I/O error occurs
*/
void format() throws IOException;

/** /**
* @return the {@link JournalWriter} for this journal * @return the {@link JournalWriter} for this journal
*/ */
Expand Down
Expand Up @@ -57,7 +57,7 @@ public class UfsJournal implements Journal {
/** The base of the entry log file names, without the file extension. */ /** The base of the entry log file names, without the file extension. */
private static final String ENTRY_LOG_FILENAME_BASE = "log"; private static final String ENTRY_LOG_FILENAME_BASE = "log";
/** The location where this journal is stored. */ /** The location where this journal is stored. */
private final URI mLocation; protected final URI mLocation;
/** The formatter for this journal. */ /** The formatter for this journal. */
private final JournalFormatter mJournalFormatter; private final JournalFormatter mJournalFormatter;


Expand All @@ -71,46 +71,6 @@ public UfsJournal(URI location) {
mJournalFormatter = JournalFormatter.Factory.create(); mJournalFormatter = JournalFormatter.Factory.create();
} }


@Override
public boolean format() throws IOException {
UnderFileSystem ufs = UnderFileSystem.Factory.get(mLocation.toString());
if (ufs.isDirectory(mLocation.toString())) {
for (UnderFileStatus p : ufs.listStatus(mLocation.toString())) {
URI childPath;
try {
childPath = URIUtils.appendPath(mLocation, p.getName());
} catch (URISyntaxException e) {
throw new RuntimeException(e.getMessage());
}
boolean failedToDelete;
if (p.isDirectory()) {
failedToDelete = !ufs.deleteDirectory(childPath.toString(),
DeleteOptions.defaults().setRecursive(true));
} else {
failedToDelete = !ufs.deleteFile(childPath.toString());
}
if (failedToDelete) {
LOG.info("Failed to delete {}", childPath);
return false;
}
}
} else if (!ufs.mkdirs(mLocation.toString())) {
LOG.info("Failed to create {}", mLocation);
return false;
}

// Create a breadcrumb that indicates that the journal folder has been formatted.
try {
UnderFileSystemUtils.touch(URIUtils.appendPath(mLocation,
Configuration.get(PropertyKey.MASTER_FORMAT_FILE_PREFIX) + System.currentTimeMillis())
.toString());
} catch (URISyntaxException e) {
throw new RuntimeException(e.getMessage());
}

return true;
}

/** /**
* @return the location of the completed logs * @return the location of the completed logs
*/ */
Expand Down
Expand Up @@ -11,10 +11,22 @@


package alluxio.master.journal.ufs; package alluxio.master.journal.ufs;


import alluxio.Configuration;
import alluxio.PropertyKey;
import alluxio.master.journal.JournalWriter; import alluxio.master.journal.JournalWriter;
import alluxio.master.journal.MutableJournal; import alluxio.master.journal.MutableJournal;
import alluxio.underfs.UnderFileStatus;
import alluxio.underfs.UnderFileSystem;
import alluxio.underfs.options.DeleteOptions;
import alluxio.util.URIUtils;
import alluxio.util.UnderFileSystemUtils;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException;


import javax.annotation.concurrent.ThreadSafe; import javax.annotation.concurrent.ThreadSafe;


Expand All @@ -23,13 +35,51 @@
*/ */
@ThreadSafe @ThreadSafe
public class UfsMutableJournal extends UfsJournal implements MutableJournal { public class UfsMutableJournal extends UfsJournal implements MutableJournal {
private static final Logger LOG = LoggerFactory.getLogger(UfsMutableJournal.class);

/** /**
* @param location the location for the journal * @param location the location for the journal
*/ */
public UfsMutableJournal(URI location) { public UfsMutableJournal(URI location) {
super(location); super(location);
} }


@Override
public void format() throws IOException {
UnderFileSystem ufs = UnderFileSystem.Factory.get(mLocation.toString());
if (ufs.isDirectory(mLocation.toString())) {
for (UnderFileStatus p : ufs.listStatus(mLocation.toString())) {
URI childPath;
try {
childPath = URIUtils.appendPath(mLocation, p.getName());
} catch (URISyntaxException e) {
throw new RuntimeException(e.getMessage());
}
boolean failedToDelete;
if (p.isDirectory()) {
failedToDelete = !ufs.deleteDirectory(childPath.toString(),
DeleteOptions.defaults().setRecursive(true));
} else {
failedToDelete = !ufs.deleteFile(childPath.toString());
}
if (failedToDelete) {
throw new IOException(String.format("Failed to delete %s", childPath));
}
}
} else if (!ufs.mkdirs(mLocation.toString())) {
throw new IOException(String.format("Failed to create %s", mLocation));
}

// Create a breadcrumb that indicates that the journal folder has been formatted.
try {
UnderFileSystemUtils.touch(URIUtils.appendPath(mLocation,
Configuration.get(PropertyKey.MASTER_FORMAT_FILE_PREFIX) + System.currentTimeMillis())
.toString());
} catch (URISyntaxException e) {
throw new RuntimeException(e.getMessage());
}
}

@Override @Override
public JournalWriter getWriter() { public JournalWriter getWriter() {
return new UfsJournalWriter(this); return new UfsJournalWriter(this);
Expand Down

0 comments on commit f0f9ccf

Please sign in to comment.