aboutsummaryrefslogtreecommitdiffstats
path: root/vespalog
diff options
context:
space:
mode:
authorArne Juul <arnej@verizonmedia.com>2020-03-31 11:49:07 +0000
committerArne Juul <arnej@verizonmedia.com>2020-04-06 12:43:06 +0000
commita9d6bfd2abf85dbedef3f492b349ceec65a255be (patch)
treee5671de2df9956c1918eef94a775024edef59c85 /vespalog
parent9ebcae1800464421a6c823635f9994e36dd68b67 (diff)
use append semantics when writing lines to logcontrol file
Diffstat (limited to 'vespalog')
-rw-r--r--vespalog/src/main/java/com/yahoo/log/VespaLevelControllerRepo.java9
-rw-r--r--vespalog/src/vespa/log/control-file.cpp50
2 files changed, 31 insertions, 28 deletions
diff --git a/vespalog/src/main/java/com/yahoo/log/VespaLevelControllerRepo.java b/vespalog/src/main/java/com/yahoo/log/VespaLevelControllerRepo.java
index 86eba1c019e..93a03bbc1b7 100644
--- a/vespalog/src/main/java/com/yahoo/log/VespaLevelControllerRepo.java
+++ b/vespalog/src/main/java/com/yahoo/log/VespaLevelControllerRepo.java
@@ -4,6 +4,7 @@ package com.yahoo.log;
import com.yahoo.text.Utf8;
import java.io.RandomAccessFile;
+import java.io.FileOutputStream;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.util.Enumeration;
@@ -11,6 +12,8 @@ import java.util.TimerTask;
import java.util.logging.LogManager;
import java.util.logging.Logger;
+import static java.nio.charset.StandardCharsets.US_ASCII;
+
/**
* @author Ulf Lilleengen
* @since 5.1
@@ -181,8 +184,12 @@ public class VespaLevelControllerRepo implements LevelControllerRepo {
sb.append(" ");
}
sb.append(inherit.getOnOffString()).append("\n");
+ byte[] lineBytes = sb.toString().getBytes(US_ASCII);
+ try (var out = new FileOutputStream(logControlFilename, true)) {
+ out.write(lineBytes);
+ out.flush();
+ }
ctlFile.seek(ctlFile.length());
- ctlFile.writeBytes(sb.toString());
extendMapping();
ctrl = levelControllerRepo.getLevelController(suffix);
} catch(java.nio.channels.ClosedByInterruptException e) {
diff --git a/vespalog/src/vespa/log/control-file.cpp b/vespalog/src/vespa/log/control-file.cpp
index a4fe9e98b8d..2f070bf088e 100644
--- a/vespalog/src/vespa/log/control-file.cpp
+++ b/vespalog/src/vespa/log/control-file.cpp
@@ -58,16 +58,14 @@ ControlFile::ensureHeader()
int wantsLen = strlen(fileHeader);
int len = read(fd, &buf, wantsLen);
if (len != wantsLen || memcmp(fileHeader, buf, wantsLen) != 0) {
- if (len) {
- if (ftruncate(fd, 0) != 0) {
- perror("log::ControlFile ftruncate failed");
- }
+ if (ftruncate(fd, 0) != 0) {
+ perror("log::ControlFile ftruncate failed");
}
lseek(fd, 0, SEEK_SET);
ssize_t nbw = write(fd, fileHeader, wantsLen);
- if (nbw != wantsLen) {
- perror("log::ControlFile write(A) failed");
- }
+ if (nbw != wantsLen) {
+ perror("log::ControlFile write(A) failed");
+ }
char spaces[_maxPrefix + 3];
memset(spaces, ' ', sizeof spaces);
@@ -75,11 +73,11 @@ ControlFile::ensureHeader()
char buf2[sizeof(spaces) + 100];
snprintf(buf2, sizeof buf2, "Prefix: \n%s\n", spaces);
- wantsLen = strlen(buf2);
+ wantsLen = strlen(buf2);
nbw = write(fd, buf2, wantsLen);
- if (nbw != wantsLen) {
- perror("log::ControlFile write(B) failed");
- }
+ if (nbw != wantsLen) {
+ perror("log::ControlFile write(B) failed");
+ }
}
}
@@ -88,10 +86,10 @@ void
ControlFile::flush()
{
if (_mapBase != NULL) {
- if (msync(_mapBase, 0, MS_SYNC) != 0) {
- LOG(warning, "msync of log control file failed: %s",
- strerror(errno));
- }
+ if (msync(_mapBase, 0, MS_SYNC) != 0) {
+ LOG(warning, "msync of log control file failed: %s",
+ strerror(errno));
+ }
}
}
@@ -259,32 +257,31 @@ ControlFile::getLevels(const char *name)
// Append whatever is in buf, excluding the initial newline, and
// up to 3 more spaces to get the entire file length to be aligned.
- int fileLength = _fileBacking.size();
+ int oldFileLength = _fileBacking.size();
char *appendedString = buf + 1;
- int newLength = fileLength + strlen(appendedString);
+ int newLength = oldFileLength + strlen(appendedString);
unsigned int padding = static_cast<unsigned int>(-newLength) & 3u;
strcat(appendedString, &padSpaces[3 - padding]);
-
- char *baseAddr = _mapBase + fileLength + strlen(appendedString);
+ int prefix_len = strlen(appendedString);
strncat(appendedString, inheritLevels, Logger::NUM_LOGLEVELS*sizeof(int));
strcat(appendedString, "\n");
int len = strlen(appendedString);
-
- int fd = _fileBacking.fd();
- lseek(fd, (off_t)fileLength, SEEK_SET);
- int wlen = write(_fileBacking.fd(), appendedString, len);
+ int fd = open(_fileName, O_WRONLY | O_APPEND);
+ int wlen = write(fd, appendedString, len);
+ oldFileLength = lseek(fd, (off_t)0, SEEK_CUR) - wlen;
+ close(fd);
if (wlen != len) {
_fileBacking.unlock();
LOG(error, "Writing to control file '%s' fails (%d/%d bytes): %s",
_fileName, wlen, len, strerror(errno));
return reinterpret_cast<unsigned int *>(inheritLevels);
} else {
- _fileSize += wlen;
+ _fileSize = _fileBacking.size();
}
- if (fileLength + wlen > _mappedSize) {
+ if (_fileSize > _mappedSize) {
if (!extendMapping()) {
_fileBacking.unlock(); // just for sure
LOG(error, "Failed to extend mapping of '%s', losing runtime "
@@ -292,9 +289,8 @@ ControlFile::getLevels(const char *name)
return defaultLevels();
}
}
-
+ char *baseAddr = _mapBase + oldFileLength + prefix_len;
_fileBacking.unlock();
-
return reinterpret_cast<unsigned int *>(baseAddr);
}