|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "encoding/hex" |
| 7 | + "encoding/json" |
| 8 | + "fmt" |
| 9 | + "io" |
| 10 | + "log" |
| 11 | + "log/slog" |
| 12 | + "os" |
| 13 | + "path/filepath" |
| 14 | + "time" |
| 15 | +) |
| 16 | + |
| 17 | +// indexEntry is the metadata that SimpleDiskCache stores on disk for an ActionID. |
| 18 | +type indexEntry struct { |
| 19 | + Version int `json:"v"` |
| 20 | + OutputID string `json:"o"` |
| 21 | + Size int64 `json:"n"` |
| 22 | + TimeNanos int64 `json:"t"` |
| 23 | +} |
| 24 | + |
| 25 | +// DiskCache is a cache that stores objects as files on disk. |
| 26 | +// |
| 27 | +// It is a fork of [github.com/bradfitz/go-tool-cache/blob/main/cachers/disk.go#DiskCache] that adds counters and more logging |
| 28 | +type DiskCache struct { |
| 29 | + Counts |
| 30 | + dir string |
| 31 | + started bool |
| 32 | + log *slog.Logger |
| 33 | +} |
| 34 | + |
| 35 | +func NewDiskCache(dir string) *DiskCache { |
| 36 | + return &DiskCache{ |
| 37 | + dir: dir, |
| 38 | + log: slog.Default().WithGroup("disk"), |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func (c *DiskCache) Start(context.Context) error { |
| 43 | + c.log.Debug("start", "dir", c.dir) |
| 44 | + err := os.MkdirAll(c.dir, 0755) |
| 45 | + if err != nil { |
| 46 | + return err |
| 47 | + } |
| 48 | + c.started = true |
| 49 | + return nil |
| 50 | +} |
| 51 | + |
| 52 | +func (c *DiskCache) Get(_ context.Context, actionID string) (outputID, diskPath string, err error) { |
| 53 | + if !c.started { |
| 54 | + log.Fatal("not started") |
| 55 | + } |
| 56 | + c.Counts.gets.Add(1) |
| 57 | + c.log.Debug("get", "actionID", actionID) |
| 58 | + actionFile := filepath.Join(c.dir, fmt.Sprintf("a-%s", actionID)) |
| 59 | + ij, err := os.ReadFile(actionFile) |
| 60 | + if err != nil { |
| 61 | + if os.IsNotExist(err) { |
| 62 | + c.Counts.misses.Add(1) |
| 63 | + return "", "", nil |
| 64 | + } |
| 65 | + c.Counts.getErrors.Add(1) |
| 66 | + return "", "", err |
| 67 | + } |
| 68 | + var ie indexEntry |
| 69 | + if err := json.Unmarshal(ij, &ie); err != nil { |
| 70 | + c.log.Error("json error", "actionID", actionID, "err", err) |
| 71 | + c.Counts.getErrors.Add(1) |
| 72 | + return "", "", nil |
| 73 | + } |
| 74 | + if _, err := hex.DecodeString(ie.OutputID); err != nil { |
| 75 | + c.Counts.getErrors.Add(1) |
| 76 | + // Protect against malicious non-hex OutputID on disk |
| 77 | + return "", "", nil |
| 78 | + } |
| 79 | + c.Counts.hits.Add(1) |
| 80 | + return ie.OutputID, filepath.Join(c.dir, fmt.Sprintf("o-%v", ie.OutputID)), nil |
| 81 | +} |
| 82 | + |
| 83 | +func (c *DiskCache) Put(_ context.Context, actionID, objectID string, size int64, body io.Reader) (diskPath string, _ error) { |
| 84 | + if !c.started { |
| 85 | + log.Fatal("not started") |
| 86 | + } |
| 87 | + c.Counts.puts.Add(1) |
| 88 | + c.log.Debug("put", "actionID", actionID, "objectID", objectID, "size", size) |
| 89 | + file := filepath.Join(c.dir, fmt.Sprintf("o-%s", objectID)) |
| 90 | + |
| 91 | + // Special case empty files; they're both common and easier to do race-free. |
| 92 | + if size == 0 { |
| 93 | + zf, err := os.OpenFile(file, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0644) |
| 94 | + if err != nil { |
| 95 | + c.Counts.putErrors.Add(1) |
| 96 | + return "", err |
| 97 | + } |
| 98 | + _ = zf.Close() |
| 99 | + } else { |
| 100 | + wrote, err := writeAtomic(file, body) |
| 101 | + if err != nil { |
| 102 | + c.Counts.putErrors.Add(1) |
| 103 | + return "", err |
| 104 | + } |
| 105 | + if wrote != size { |
| 106 | + c.Counts.putErrors.Add(1) |
| 107 | + return "", fmt.Errorf("wrote %d bytes, expected %d", wrote, size) |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + ij, err := json.Marshal(indexEntry{ |
| 112 | + Version: 1, |
| 113 | + OutputID: objectID, |
| 114 | + Size: size, |
| 115 | + TimeNanos: time.Now().UnixNano(), |
| 116 | + }) |
| 117 | + if err != nil { |
| 118 | + c.Counts.putErrors.Add(1) |
| 119 | + return "", err |
| 120 | + } |
| 121 | + actionFile := filepath.Join(c.dir, fmt.Sprintf("a-%s", actionID)) |
| 122 | + if _, err := writeAtomic(actionFile, bytes.NewReader(ij)); err != nil { |
| 123 | + c.Counts.putErrors.Add(1) |
| 124 | + return "", err |
| 125 | + } |
| 126 | + return file, nil |
| 127 | +} |
| 128 | + |
| 129 | +func (c *DiskCache) Close() error { |
| 130 | + if !c.started { |
| 131 | + log.Fatal("not started") |
| 132 | + } |
| 133 | + c.started = false |
| 134 | + c.log.Debug("close") |
| 135 | + return nil |
| 136 | +} |
| 137 | + |
| 138 | +func writeTempFile(dest string, r io.Reader) (string, int64, error) { |
| 139 | + tf, err := os.CreateTemp(filepath.Dir(dest), filepath.Base(dest)+".*") |
| 140 | + if err != nil { |
| 141 | + return "", 0, err |
| 142 | + } |
| 143 | + fileName := tf.Name() |
| 144 | + defer func() { |
| 145 | + _ = tf.Close() |
| 146 | + if err != nil { |
| 147 | + _ = os.Remove(fileName) |
| 148 | + } |
| 149 | + }() |
| 150 | + size, err := io.Copy(tf, r) |
| 151 | + if err != nil { |
| 152 | + return "", 0, err |
| 153 | + } |
| 154 | + return fileName, size, nil |
| 155 | +} |
| 156 | + |
| 157 | +func writeAtomic(dest string, r io.Reader) (int64, error) { |
| 158 | + tempFile, size, err := writeTempFile(dest, r) |
| 159 | + if err != nil { |
| 160 | + return 0, err |
| 161 | + } |
| 162 | + defer func() { |
| 163 | + if err != nil { |
| 164 | + _ = os.Remove(tempFile) |
| 165 | + } |
| 166 | + }() |
| 167 | + if err = os.Rename(tempFile, dest); err != nil { |
| 168 | + return 0, err |
| 169 | + } |
| 170 | + return size, nil |
| 171 | +} |
0 commit comments