summaryrefslogtreecommitdiffstats
path: root/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/file/FileSync.java
blob: 78f720074dc8176cb50ced7daaf53e31895afbe4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

package com.yahoo.vespa.hosted.node.admin.task.util.file;

import com.yahoo.vespa.hosted.node.admin.component.TaskContext;

import java.nio.file.Path;
import java.util.Arrays;
import java.util.Optional;
import java.util.logging.Logger;

/**
 * Class to minimize resource usage with repetitive and mostly identical, idempotent, and
 * mutating file operations, e.g. setting file content, setting owner, etc.
 *
 * Only changes to the file is logged.
 *
 * @author hakohall
 */
// @ThreadUnsafe
public class FileSync {
    private static final Logger logger = Logger.getLogger(FileSync.class.getName());

    private final UnixPath path;
    private final FileContentCache contentCache;

    public FileSync(Path path) {
        this.path = new UnixPath(path);
        this.contentCache = new FileContentCache(this.path);
    }

    /**
     * CPU, I/O, and memory usage is optimized for repeated calls with the same arguments.
     * @return true if the system was modified: content was written, or owner was set, etc.
     *         system is only modified if necessary (different).
     */
    public boolean convergeTo(TaskContext taskContext, PartialFileData partialFileData) {
        FileAttributesCache currentAttributes = new FileAttributesCache(path);

        boolean modifiedSystem = maybeUpdateContent(taskContext, partialFileData.getContent(), currentAttributes);

        AttributeSync attributeSync = new AttributeSync(path.toPath()).with(partialFileData);
        modifiedSystem |= attributeSync.converge(taskContext, currentAttributes);

        return modifiedSystem;
    }

    private boolean maybeUpdateContent(TaskContext taskContext,
                                       Optional<byte[]> content,
                                       FileAttributesCache currentAttributes) {
        if (!content.isPresent()) {
            return false;
        }

        if (!currentAttributes.exists()) {
            taskContext.recordSystemModification(logger, "Creating file " + path);
            path.createParents();
            path.writeBytes(content.get());
            contentCache.updateWith(content.get(), currentAttributes.forceGet().lastModifiedTime());
            return true;
        }

        if (Arrays.equals(content.get(), contentCache.get(currentAttributes.get().lastModifiedTime()))) {
            return false;
        } else {
            taskContext.recordSystemModification(logger, "Patching file " + path);
            path.writeBytes(content.get());
            contentCache.updateWith(content.get(), currentAttributes.forceGet().lastModifiedTime());
            return true;
        }
    }
}