Skip to content

Commit

Permalink
Temp Workaround to recovery from bad string size, apache#432
Browse files Browse the repository at this point in the history
Since the registry data is invalid, i simply empty the content to guarantee the next registry will work correct.
  • Loading branch information
lanmaoxinqing committed Jun 17, 2021
1 parent 7c8f178 commit e5cc0d0
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
22 changes: 19 additions & 3 deletions common/src/main/java/org/mvndaemon/mvnd/common/DaemonRegistry.java
Expand Up @@ -16,7 +16,9 @@

package org.mvndaemon.mvnd.common;

import java.io.File;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.nio.MappedByteBuffer;
Expand All @@ -35,8 +37,6 @@
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static org.mvndaemon.mvnd.common.DaemonState.Canceled;
import static org.mvndaemon.mvnd.common.DaemonState.Idle;
Expand Down Expand Up @@ -242,12 +242,28 @@ private void doUpdate(Runnable updater) {
return;
} catch (IOException e) {
throw new RuntimeException("Could not lock offset 0 of " + registryFile);
} catch (IllegalStateException e) {
String absPath = registryFile.toAbsolutePath().normalize().toString();
LOGGER.warn("Invalid daemon registry info, " +
"try to recovery this issue by reset registry buffer. " +
"If you keep getting this warning, " +
"try to delete `registry.bin` file at [" + absPath + "]", e);
this.reset();
return;
}
}
throw new RuntimeException("Could not lock " + registryFile + " within " + LOCK_TIMEOUT_MS + " ms");
}
}

private void reset() {
infosMap.clear();
stopEvents.clear();
BufferCaster.cast(buffer).clear();
buffer.putInt(0); // reset daemon count
buffer.putInt(0); // reset stop event count
}

private static final int PROCESS_ID = getProcessId0();

private static int getProcessId0() {
Expand Down
Expand Up @@ -15,13 +15,14 @@
*/
package org.mvndaemon.mvnd.common;

import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Locale;
import java.util.Random;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
Expand Down Expand Up @@ -50,7 +51,32 @@ public void testReadWrite() throws IOException {
assertNotNull(reg2.getAll());
assertEquals(1, reg2.getAll().size());
}
}

@Test
public void testRecovery() throws IOException {
Path temp = File.createTempFile("reg", ".data").toPath();
temp.toFile().deleteOnExit();
try (DaemonRegistry reg1 = new DaemonRegistry(temp)) {
// first store daemon
byte[] token = new byte[16];
new Random().nextBytes(token);
reg1.store(new DaemonInfo("12345678", "/java/home/",
"/data/reg/", 0x12345678, 7502, token,
Locale.getDefault().toLanguageTag(), Arrays.asList("-Xmx"),
DaemonState.Idle, System.currentTimeMillis(), System.currentTimeMillis()));
assertEquals(1, reg1.getAll().size());
// store an invalid event to trigger recovery
reg1.storeStopEvent(new DaemonStopEvent("11111",
System.currentTimeMillis(),
DaemonExpirationStatus.QUIET_EXPIRE,
"reason txt longer than 1024, reason txt longer than 1024, reason txt longer than 1024, reason txt longer than 1024, reason txt longer than 1024, reason txt longer than 1024, reason txt longer than 1024, reason txt longer than 1024, reason txt longer than 1024, reason txt longer than 1024, reason txt longer than 1024, reason txt longer than 1024, reason txt longer than 1024, reason txt longer than 1024, reason txt longer than 1024, reason txt longer than 1024, reason txt longer than 1024, reason txt longer than 1024, reason txt longer than 1024, reason txt longer than 1024, reason txt longer than 1024, reason txt longer than 1024, reason txt longer than 1024, reason txt longer than 1024, reason txt longer than 1024, reason txt longer than 1024, reason txt longer than 1024, reason txt longer than 1024, reason txt longer than 1024, reason txt longer than 1024, reason txt longer than 1024, reason txt longer than 1024, reason txt longer than 1024, reason txt longer than 1024, reason txt longer than 1024, reason txt longer than 1024, reason txt longer than 1024"
));
// check if registry is reset
assertEquals(0, reg1.getAll().size());
}
}



}

0 comments on commit e5cc0d0

Please sign in to comment.