/** * Tests choosing of storage directory when only one volume is configured. * * @throws IOException in case of exception. */ @Test public void testChooseStorageDirWithOneVolume() throws IOException { File testDir = new File(rootTestDir.get(), UUID.randomUUID().toString()); List<File> directories = Collections.singletonList(testDir); String subDirOne = UUID.randomUUID().toString(); String subDirTwo = UUID.randomUUID().toString(); File storageDirOne = ServerState.chooseStorageDir(directories, subDirOne); File storageDirTwo = ServerState.chooseStorageDir(directories, subDirTwo); File expectedOne = new File(testDir, subDirOne); File expectedTwo = new File(testDir, subDirTwo); Assert.assertEquals(expectedOne.getCanonicalPath(), storageDirOne.getCanonicalPath()); Assert.assertEquals(expectedTwo.getCanonicalPath(), storageDirTwo.getCanonicalPath()); }
/** * Tests choosing of storage directory when only no volume is configured. * * @throws IOException in case of exception. */ @Test public void testChooseStorageDirWithNoVolume() { try { ServerState.chooseStorageDir( Collections.emptyList(), UUID.randomUUID().toString()); Assert.fail(); } catch (IOException ex) { String expectedErrMsg = "No storage directory found."; Assert.assertEquals(expectedErrMsg, ex.getMessage()); } }
File storageDirectory = ServerState.chooseStorageDir(directories, subDir); File expected = new File(directories.get(6), subDir); Assert.assertEquals(expected.getCanonicalPath(),
ServerState(RaftPeerId id, RaftGroup group, RaftProperties prop, RaftServerImpl server, StateMachine stateMachine) throws IOException { this.selfId = id; this.server = server; RaftConfiguration initialConf = RaftConfiguration.newBuilder() .setConf(group.getPeers()).build(); configurationManager = new ConfigurationManager(initialConf); LOG.info("{}: {}", id, configurationManager); // use full uuid string to create a subdirectory final File dir = chooseStorageDir(RaftServerConfigKeys.storageDirs(prop), group.getGroupId().getUuid().toString()); storage = new RaftStorage(dir, RaftServerConstants.StartupOption.REGULAR); snapshotManager = new SnapshotManager(storage, id); long lastApplied = initStatemachine(stateMachine, group.getGroupId()); // On start the leader is null, start the clock now leaderId = null; this.lastNoLeaderTime = Timestamp.currentTime(); this.leaderElectionTimeoutMs = RaftServerConfigKeys.leaderElectionTimeout(prop).toIntExact(TimeUnit.MILLISECONDS); // we cannot apply log entries to the state machine in this step, since we // do not know whether the local log entries have been committed. log = initLog(id, prop, lastApplied, this::setRaftConf); RaftLog.Metadata metadata = log.loadMetadata(); currentTerm = metadata.getTerm(); votedFor = metadata.getVotedFor(); stateMachineUpdater = new StateMachineUpdater(stateMachine, server, log, lastApplied, prop); }