log the file name when exceptions are thrown during i/o

This commit is contained in:
Luck 2017-04-12 14:09:33 +01:00
parent c7305f6b06
commit 71bfdd9bfd
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
3 changed files with 180 additions and 148 deletions

View File

@ -63,15 +63,6 @@ import java.util.stream.Collectors;
public abstract class FlatfileBacking extends AbstractBacking { public abstract class FlatfileBacking extends AbstractBacking {
private static final String LOG_FORMAT = "%s(%s): [%s] %s(%s) --> %s"; private static final String LOG_FORMAT = "%s(%s): [%s] %s(%s) --> %s";
protected static <T> T call(Callable<T> c, T def) {
try {
return c.call();
} catch (Exception e) {
e.printStackTrace();
return def;
}
}
private final Logger actionLogger = Logger.getLogger("lp_actions"); private final Logger actionLogger = Logger.getLogger("lp_actions");
private Map<String, String> uuidCache = new ConcurrentHashMap<>(); private Map<String, String> uuidCache = new ConcurrentHashMap<>();
@ -216,6 +207,16 @@ public abstract class FlatfileBacking extends AbstractBacking {
plugin.applyToFileWatcher(fileWatcher -> fileWatcher.registerChange(type, file.getName())); plugin.applyToFileWatcher(fileWatcher -> fileWatcher.registerChange(type, file.getName()));
} }
protected <T> T call(String file, Callable<T> c, T def) {
try {
return c.call();
} catch (Exception e) {
plugin.getLog().warn("Exception thrown whilst performing i/o: " + file);
e.printStackTrace();
return def;
}
}
@Override @Override
public boolean logAction(LogEntry entry) { public boolean logAction(LogEntry entry) {
actionLogger.info(String.format(LOG_FORMAT, actionLogger.info(String.format(LOG_FORMAT,
@ -266,7 +267,7 @@ public abstract class FlatfileBacking extends AbstractBacking {
public boolean deleteGroup(Group group) { public boolean deleteGroup(Group group) {
group.getIoLock().lock(); group.getIoLock().lock();
try { try {
return call(() -> { return call(group.getName(), () -> {
File groupFile = new File(groupsDir, group.getName() + fileExtension); File groupFile = new File(groupsDir, group.getName() + fileExtension);
registerFileAction("groups", groupFile); registerFileAction("groups", groupFile);
@ -301,7 +302,7 @@ public abstract class FlatfileBacking extends AbstractBacking {
public boolean deleteTrack(Track track) { public boolean deleteTrack(Track track) {
track.getIoLock().lock(); track.getIoLock().lock();
try { try {
return call(() -> { return call(track.getName(), () -> {
File trackFile = new File(tracksDir, track.getName() + fileExtension); File trackFile = new File(tracksDir, track.getName() + fileExtension);
registerFileAction("tracks", trackFile); registerFileAction("tracks", trackFile);

View File

@ -94,12 +94,13 @@ public class JSONBacking extends FlatfileBacking {
@Override @Override
public boolean applyBulkUpdate(BulkUpdate bulkUpdate) { public boolean applyBulkUpdate(BulkUpdate bulkUpdate) {
return call(() -> { return call("null", () -> {
if (bulkUpdate.getDataType().isIncludingUsers()) { if (bulkUpdate.getDataType().isIncludingUsers()) {
File[] files = usersDir.listFiles((dir, name1) -> name1.endsWith(".json")); File[] files = usersDir.listFiles((dir, name1) -> name1.endsWith(".json"));
if (files == null) return false; if (files == null) return false;
for (File file : files) { for (File file : files) {
call(file.getName(), () -> {
registerFileAction("users", file); registerFileAction("users", file);
JsonObject object = readObjectFromFile(file); JsonObject object = readObjectFromFile(file);
@ -116,6 +117,8 @@ public class JSONBacking extends FlatfileBacking {
object.add("permissions", serializePermissions(results)); object.add("permissions", serializePermissions(results));
writeElementToFile(file, object); writeElementToFile(file, object);
return true;
}, true);
} }
} }
@ -124,6 +127,7 @@ public class JSONBacking extends FlatfileBacking {
if (files == null) return false; if (files == null) return false;
for (File file : files) { for (File file : files) {
call(file.getName(), () -> {
registerFileAction("groups", file); registerFileAction("groups", file);
JsonObject object = readObjectFromFile(file); JsonObject object = readObjectFromFile(file);
@ -140,6 +144,8 @@ public class JSONBacking extends FlatfileBacking {
object.add("permissions", serializePermissions(results)); object.add("permissions", serializePermissions(results));
writeElementToFile(file, object); writeElementToFile(file, object);
return true;
}, true);
} }
} }
@ -152,7 +158,7 @@ public class JSONBacking extends FlatfileBacking {
User user = plugin.getUserManager().getOrMake(UserIdentifier.of(uuid, username)); User user = plugin.getUserManager().getOrMake(UserIdentifier.of(uuid, username));
user.getIoLock().lock(); user.getIoLock().lock();
try { try {
return call(() -> { return call(uuid.toString(), () -> {
File userFile = new File(usersDir, uuid.toString() + ".json"); File userFile = new File(usersDir, uuid.toString() + ".json");
registerFileAction("users", userFile); registerFileAction("users", userFile);
@ -199,7 +205,7 @@ public class JSONBacking extends FlatfileBacking {
public boolean saveUser(User user) { public boolean saveUser(User user) {
user.getIoLock().lock(); user.getIoLock().lock();
try { try {
return call(() -> { return call(user.getUuid().toString(), () -> {
File userFile = new File(usersDir, user.getUuid().toString() + ".json"); File userFile = new File(usersDir, user.getUuid().toString() + ".json");
registerFileAction("users", userFile); registerFileAction("users", userFile);
@ -236,11 +242,12 @@ public class JSONBacking extends FlatfileBacking {
@Override @Override
public boolean cleanupUsers() { public boolean cleanupUsers() {
return call(() -> { return call("null", () -> {
File[] files = usersDir.listFiles((dir, name1) -> name1.endsWith(".json")); File[] files = usersDir.listFiles((dir, name1) -> name1.endsWith(".json"));
if (files == null) return false; if (files == null) return false;
for (File file : files) { for (File file : files) {
call(file.getName(), () -> {
registerFileAction("users", file); registerFileAction("users", file);
JsonObject object = readObjectFromFile(file); JsonObject object = readObjectFromFile(file);
@ -259,6 +266,8 @@ public class JSONBacking extends FlatfileBacking {
if (shouldDelete) { if (shouldDelete) {
file.delete(); file.delete();
} }
return true;
}, true);
} }
return true; return true;
}, false); }, false);
@ -267,11 +276,12 @@ public class JSONBacking extends FlatfileBacking {
@Override @Override
public List<HeldPermission<UUID>> getUsersWithPermission(String permission) { public List<HeldPermission<UUID>> getUsersWithPermission(String permission) {
ImmutableList.Builder<HeldPermission<UUID>> held = ImmutableList.builder(); ImmutableList.Builder<HeldPermission<UUID>> held = ImmutableList.builder();
boolean success = call(() -> { boolean success = call("null", () -> {
File[] files = usersDir.listFiles((dir, name1) -> name1.endsWith(".json")); File[] files = usersDir.listFiles((dir, name1) -> name1.endsWith(".json"));
if (files == null) return false; if (files == null) return false;
for (File file : files) { for (File file : files) {
call(file.getName(), () -> {
registerFileAction("users", file); registerFileAction("users", file);
UUID holder = UUID.fromString(file.getName().substring(0, file.getName().length() - 5)); UUID holder = UUID.fromString(file.getName().substring(0, file.getName().length() - 5));
@ -288,6 +298,8 @@ public class JSONBacking extends FlatfileBacking {
held.add(NodeHeldPermission.of(holder, e)); held.add(NodeHeldPermission.of(holder, e));
} }
return true;
}, true);
} }
return true; return true;
}, false); }, false);
@ -299,7 +311,7 @@ public class JSONBacking extends FlatfileBacking {
Group group = plugin.getGroupManager().getOrMake(name); Group group = plugin.getGroupManager().getOrMake(name);
group.getIoLock().lock(); group.getIoLock().lock();
try { try {
return call(() -> { return call(name, () -> {
File groupFile = new File(groupsDir, name + ".json"); File groupFile = new File(groupsDir, name + ".json");
registerFileAction("groups", groupFile); registerFileAction("groups", groupFile);
@ -336,7 +348,7 @@ public class JSONBacking extends FlatfileBacking {
Group group = plugin.getGroupManager().getOrMake(name); Group group = plugin.getGroupManager().getOrMake(name);
group.getIoLock().lock(); group.getIoLock().lock();
try { try {
return call(() -> { return call(name, () -> {
File groupFile = new File(groupsDir, name + ".json"); File groupFile = new File(groupsDir, name + ".json");
registerFileAction("groups", groupFile); registerFileAction("groups", groupFile);
@ -359,7 +371,7 @@ public class JSONBacking extends FlatfileBacking {
public boolean saveGroup(Group group) { public boolean saveGroup(Group group) {
group.getIoLock().lock(); group.getIoLock().lock();
try { try {
return call(() -> { return call(group.getName(), () -> {
File groupFile = new File(groupsDir, group.getName() + ".json"); File groupFile = new File(groupsDir, group.getName() + ".json");
registerFileAction("groups", groupFile); registerFileAction("groups", groupFile);
@ -386,11 +398,12 @@ public class JSONBacking extends FlatfileBacking {
@Override @Override
public List<HeldPermission<String>> getGroupsWithPermission(String permission) { public List<HeldPermission<String>> getGroupsWithPermission(String permission) {
ImmutableList.Builder<HeldPermission<String>> held = ImmutableList.builder(); ImmutableList.Builder<HeldPermission<String>> held = ImmutableList.builder();
boolean success = call(() -> { boolean success = call("null", () -> {
File[] files = groupsDir.listFiles((dir, name1) -> name1.endsWith(".json")); File[] files = groupsDir.listFiles((dir, name1) -> name1.endsWith(".json"));
if (files == null) return false; if (files == null) return false;
for (File file : files) { for (File file : files) {
call(file.getName(), () -> {
registerFileAction("groups", file); registerFileAction("groups", file);
String holder = file.getName().substring(0, file.getName().length() - 5); String holder = file.getName().substring(0, file.getName().length() - 5);
@ -407,6 +420,8 @@ public class JSONBacking extends FlatfileBacking {
held.add(NodeHeldPermission.of(holder, e)); held.add(NodeHeldPermission.of(holder, e));
} }
return true;
}, true);
} }
return true; return true;
}, false); }, false);
@ -418,7 +433,7 @@ public class JSONBacking extends FlatfileBacking {
Track track = plugin.getTrackManager().getOrMake(name); Track track = plugin.getTrackManager().getOrMake(name);
track.getIoLock().lock(); track.getIoLock().lock();
try { try {
return call(() -> { return call(name, () -> {
File trackFile = new File(tracksDir, name + ".json"); File trackFile = new File(tracksDir, name + ".json");
registerFileAction("tracks", trackFile); registerFileAction("tracks", trackFile);
@ -459,7 +474,7 @@ public class JSONBacking extends FlatfileBacking {
Track track = plugin.getTrackManager().getOrMake(name); Track track = plugin.getTrackManager().getOrMake(name);
track.getIoLock().lock(); track.getIoLock().lock();
try { try {
return call(() -> { return call(name, () -> {
File trackFile = new File(tracksDir, name + ".json"); File trackFile = new File(tracksDir, name + ".json");
registerFileAction("tracks", trackFile); registerFileAction("tracks", trackFile);
@ -485,7 +500,7 @@ public class JSONBacking extends FlatfileBacking {
public boolean saveTrack(Track track) { public boolean saveTrack(Track track) {
track.getIoLock().lock(); track.getIoLock().lock();
try { try {
return call(() -> { return call(track.getName(), () -> {
File trackFile = new File(tracksDir, track.getName() + ".json"); File trackFile = new File(tracksDir, track.getName() + ".json");
registerFileAction("tracks", trackFile); registerFileAction("tracks", trackFile);

View File

@ -98,12 +98,13 @@ public class YAMLBacking extends FlatfileBacking {
@Override @Override
public boolean applyBulkUpdate(BulkUpdate bulkUpdate) { public boolean applyBulkUpdate(BulkUpdate bulkUpdate) {
return call(() -> { return call("null", () -> {
if (bulkUpdate.getDataType().isIncludingUsers()) { if (bulkUpdate.getDataType().isIncludingUsers()) {
File[] files = usersDir.listFiles((dir, name1) -> name1.endsWith(".yml")); File[] files = usersDir.listFiles((dir, name1) -> name1.endsWith(".yml"));
if (files == null) return false; if (files == null) return false;
for (File file : files) { for (File file : files) {
call(file.getName(), () -> {
registerFileAction("users", file); registerFileAction("users", file);
Map<String, Object> values = readMapFromFile(file); Map<String, Object> values = readMapFromFile(file);
@ -119,6 +120,8 @@ public class YAMLBacking extends FlatfileBacking {
values.put("permissions", serializePermissions(results)); values.put("permissions", serializePermissions(results));
writeMapToFile(file, values); writeMapToFile(file, values);
return true;
}, true);
} }
} }
@ -127,6 +130,7 @@ public class YAMLBacking extends FlatfileBacking {
if (files == null) return false; if (files == null) return false;
for (File file : files) { for (File file : files) {
call(file.getName(), () -> {
registerFileAction("groups", file); registerFileAction("groups", file);
Map<String, Object> values = readMapFromFile(file); Map<String, Object> values = readMapFromFile(file);
@ -142,6 +146,8 @@ public class YAMLBacking extends FlatfileBacking {
values.put("permissions", serializePermissions(results)); values.put("permissions", serializePermissions(results));
writeMapToFile(file, values); writeMapToFile(file, values);
return true;
}, true);
} }
} }
@ -154,7 +160,7 @@ public class YAMLBacking extends FlatfileBacking {
User user = plugin.getUserManager().getOrMake(UserIdentifier.of(uuid, username)); User user = plugin.getUserManager().getOrMake(UserIdentifier.of(uuid, username));
user.getIoLock().lock(); user.getIoLock().lock();
try { try {
return call(() -> { return call(uuid.toString(), () -> {
File userFile = new File(usersDir, uuid.toString() + ".yml"); File userFile = new File(usersDir, uuid.toString() + ".yml");
registerFileAction("users", userFile); registerFileAction("users", userFile);
if (userFile.exists()) { if (userFile.exists()) {
@ -201,7 +207,7 @@ public class YAMLBacking extends FlatfileBacking {
public boolean saveUser(User user) { public boolean saveUser(User user) {
user.getIoLock().lock(); user.getIoLock().lock();
try { try {
return call(() -> { return call(user.getUuid().toString(), () -> {
File userFile = new File(usersDir, user.getUuid().toString() + ".yml"); File userFile = new File(usersDir, user.getUuid().toString() + ".yml");
registerFileAction("users", userFile); registerFileAction("users", userFile);
if (!GenericUserManager.shouldSave(user)) { if (!GenericUserManager.shouldSave(user)) {
@ -237,11 +243,12 @@ public class YAMLBacking extends FlatfileBacking {
@Override @Override
public boolean cleanupUsers() { public boolean cleanupUsers() {
return call(() -> { return call("null", () -> {
File[] files = usersDir.listFiles((dir, name1) -> name1.endsWith(".yml")); File[] files = usersDir.listFiles((dir, name1) -> name1.endsWith(".yml"));
if (files == null) return false; if (files == null) return false;
for (File file : files) { for (File file : files) {
call(file.getName(), () -> {
registerFileAction("users", file); registerFileAction("users", file);
Map<String, Object> values = readMapFromFile(file); Map<String, Object> values = readMapFromFile(file);
@ -260,6 +267,8 @@ public class YAMLBacking extends FlatfileBacking {
if (shouldDelete) { if (shouldDelete) {
file.delete(); file.delete();
} }
return true;
}, true);
} }
return true; return true;
}, false); }, false);
@ -268,11 +277,12 @@ public class YAMLBacking extends FlatfileBacking {
@Override @Override
public List<HeldPermission<UUID>> getUsersWithPermission(String permission) { public List<HeldPermission<UUID>> getUsersWithPermission(String permission) {
ImmutableList.Builder<HeldPermission<UUID>> held = ImmutableList.builder(); ImmutableList.Builder<HeldPermission<UUID>> held = ImmutableList.builder();
boolean success = call(() -> { boolean success = call("null", () -> {
File[] files = usersDir.listFiles((dir, name1) -> name1.endsWith(".yml")); File[] files = usersDir.listFiles((dir, name1) -> name1.endsWith(".yml"));
if (files == null) return false; if (files == null) return false;
for (File file : files) { for (File file : files) {
call(file.getName(), () -> {
registerFileAction("users", file); registerFileAction("users", file);
UUID holder = UUID.fromString(file.getName().substring(0, file.getName().length() - 4)); UUID holder = UUID.fromString(file.getName().substring(0, file.getName().length() - 4));
@ -289,6 +299,8 @@ public class YAMLBacking extends FlatfileBacking {
held.add(NodeHeldPermission.of(holder, e)); held.add(NodeHeldPermission.of(holder, e));
} }
return true;
}, true);
} }
return true; return true;
}, false); }, false);
@ -300,7 +312,7 @@ public class YAMLBacking extends FlatfileBacking {
Group group = plugin.getGroupManager().getOrMake(name); Group group = plugin.getGroupManager().getOrMake(name);
group.getIoLock().lock(); group.getIoLock().lock();
try { try {
return call(() -> { return call(name, () -> {
File groupFile = new File(groupsDir, name + ".yml"); File groupFile = new File(groupsDir, name + ".yml");
registerFileAction("groups", groupFile); registerFileAction("groups", groupFile);
@ -335,7 +347,7 @@ public class YAMLBacking extends FlatfileBacking {
Group group = plugin.getGroupManager().getOrMake(name); Group group = plugin.getGroupManager().getOrMake(name);
group.getIoLock().lock(); group.getIoLock().lock();
try { try {
return call(() -> { return call(name, () -> {
File groupFile = new File(groupsDir, name + ".yml"); File groupFile = new File(groupsDir, name + ".yml");
registerFileAction("groups", groupFile); registerFileAction("groups", groupFile);
@ -358,7 +370,7 @@ public class YAMLBacking extends FlatfileBacking {
public boolean saveGroup(Group group) { public boolean saveGroup(Group group) {
group.getIoLock().lock(); group.getIoLock().lock();
try { try {
return call(() -> { return call(group.getName(), () -> {
File groupFile = new File(groupsDir, group.getName() + ".yml"); File groupFile = new File(groupsDir, group.getName() + ".yml");
registerFileAction("groups", groupFile); registerFileAction("groups", groupFile);
@ -385,11 +397,12 @@ public class YAMLBacking extends FlatfileBacking {
@Override @Override
public List<HeldPermission<String>> getGroupsWithPermission(String permission) { public List<HeldPermission<String>> getGroupsWithPermission(String permission) {
ImmutableList.Builder<HeldPermission<String>> held = ImmutableList.builder(); ImmutableList.Builder<HeldPermission<String>> held = ImmutableList.builder();
boolean success = call(() -> { boolean success = call("null", () -> {
File[] files = groupsDir.listFiles((dir, name1) -> name1.endsWith(".yml")); File[] files = groupsDir.listFiles((dir, name1) -> name1.endsWith(".yml"));
if (files == null) return false; if (files == null) return false;
for (File file : files) { for (File file : files) {
call(file.getName(), () -> {
registerFileAction("groups", file); registerFileAction("groups", file);
String holder = file.getName().substring(0, file.getName().length() - 4); String holder = file.getName().substring(0, file.getName().length() - 4);
@ -406,6 +419,9 @@ public class YAMLBacking extends FlatfileBacking {
held.add(NodeHeldPermission.of(holder, e)); held.add(NodeHeldPermission.of(holder, e));
} }
return true;
}, true);
} }
return true; return true;
}, false); }, false);
@ -417,7 +433,7 @@ public class YAMLBacking extends FlatfileBacking {
Track track = plugin.getTrackManager().getOrMake(name); Track track = plugin.getTrackManager().getOrMake(name);
track.getIoLock().lock(); track.getIoLock().lock();
try { try {
return call(() -> { return call(name, () -> {
File trackFile = new File(tracksDir, name + ".yml"); File trackFile = new File(tracksDir, name + ".yml");
registerFileAction("tracks", trackFile); registerFileAction("tracks", trackFile);
@ -450,7 +466,7 @@ public class YAMLBacking extends FlatfileBacking {
Track track = plugin.getTrackManager().getOrMake(name); Track track = plugin.getTrackManager().getOrMake(name);
track.getIoLock().lock(); track.getIoLock().lock();
try { try {
return call(() -> { return call(name, () -> {
File trackFile = new File(tracksDir, name + ".yml"); File trackFile = new File(tracksDir, name + ".yml");
registerFileAction("tracks", trackFile); registerFileAction("tracks", trackFile);
@ -471,7 +487,7 @@ public class YAMLBacking extends FlatfileBacking {
public boolean saveTrack(Track track) { public boolean saveTrack(Track track) {
track.getIoLock().lock(); track.getIoLock().lock();
try { try {
return call(() -> { return call(track.getName(), () -> {
File trackFile = new File(tracksDir, track.getName() + ".yml"); File trackFile = new File(tracksDir, track.getName() + ".yml");
registerFileAction("tracks", trackFile); registerFileAction("tracks", trackFile);