|
| 1 | +/* |
| 2 | +Testing automatic tiered storage and backup persistence functionality. |
| 3 | +*/ |
| 4 | + |
| 5 | +import { |
| 6 | + before, |
| 7 | + after, |
| 8 | + connect, |
| 9 | + delay, |
| 10 | + client, |
| 11 | + wait, |
| 12 | +} from "@cocalc/backend/conat/test/setup"; |
| 13 | +import { stream } from "@cocalc/conat/persist/client"; |
| 14 | +import { syncFiles } from "@cocalc/conat/persist/context"; |
| 15 | +import { pathExists } from "fs-extra"; |
| 16 | +import { join } from "path"; |
| 17 | +import * as fs from "fs/promises"; |
| 18 | +import { messageData } from "@cocalc/conat/core/client"; |
| 19 | +import { executeCode } from "@cocalc/backend/execute-code"; |
| 20 | +import sqlite from "better-sqlite3"; |
| 21 | +import { openPaths } from "@cocalc/conat/persist/storage"; |
| 22 | + |
| 23 | +beforeAll(async () => { |
| 24 | + await before({ archive: "archive", backup: "backup", archiveInterval: 250 }); |
| 25 | +}); |
| 26 | + |
| 27 | +describe("create persist server that also saves data to an archive folder and a backup folder", () => { |
| 28 | + it("verify that archive, backup and archiveInterval are all configured", async () => { |
| 29 | + expect(syncFiles.archive).toContain("archive"); |
| 30 | + expect(syncFiles.archiveInterval).toBeGreaterThan(0); |
| 31 | + expect(syncFiles.backup).toContain("backup"); |
| 32 | + }); |
| 33 | + |
| 34 | + async function waitUntilClosed() { |
| 35 | + await wait({ |
| 36 | + until: () => { |
| 37 | + return !openPaths.has(join(syncFiles.local, "hub/foo")); |
| 38 | + }, |
| 39 | + }); |
| 40 | + } |
| 41 | + |
| 42 | + let s1; |
| 43 | + it("create a new stream", async () => { |
| 44 | + s1 = stream({ |
| 45 | + client, |
| 46 | + user: { hub_id: "x" }, |
| 47 | + storage: { path: "hub/foo" }, |
| 48 | + }); |
| 49 | + await s1.set({ |
| 50 | + key: "my-key-1", |
| 51 | + messageData: messageData("one"), |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + let local, archive, backup; |
| 56 | + it(`wait, then there is an updated archive file too`, async () => { |
| 57 | + ((local = join(syncFiles.local, "hub/foo.db")), |
| 58 | + (archive = join(syncFiles.archive, "hub/foo.db")), |
| 59 | + (backup = join(syncFiles.backup, "hub/foo.db")), |
| 60 | + expect(await pathExists(local)).toBe(true)); |
| 61 | + // gets created initially |
| 62 | + expect(await pathExists(archive)).toBe(true); |
| 63 | + // backup should only exist when stream is closed |
| 64 | + expect(await pathExists(backup)).toBe(false); |
| 65 | + |
| 66 | + // timestamp before another write |
| 67 | + const stats = await fs.stat(archive); |
| 68 | + |
| 69 | + await s1.set({ |
| 70 | + key: "my-key-2", |
| 71 | + messageData: messageData("two"), |
| 72 | + }); |
| 73 | + // now wait to ensure archive gets written |
| 74 | + |
| 75 | + await delay(syncFiles.archiveInterval + 100); |
| 76 | + expect(await pathExists(archive)).toBe(true); |
| 77 | + const stats2 = await fs.stat(archive); |
| 78 | + expect(stats2.mtimeMs).not.toEqual(stats.mtimeMs); |
| 79 | + }); |
| 80 | + |
| 81 | + it("close the stream and see that the backup and archive are both written, even though we didn't wait the full archive interval", async () => { |
| 82 | + s1.close(); |
| 83 | + const t = Date.now(); |
| 84 | + await wait({ |
| 85 | + until: async () => await pathExists(backup), |
| 86 | + }); |
| 87 | + expect(Date.now() - t).toBeLessThan(syncFiles.archiveInterval); |
| 88 | + expect(await pathExists(backup)).toBe(true); |
| 89 | + // at this point the actual sqlite3 database should be closed |
| 90 | + }); |
| 91 | + |
| 92 | + const sha1 = async (path) => { |
| 93 | + const { stdout } = await executeCode({ command: "sha1sum", args: [path] }); |
| 94 | + return stdout; |
| 95 | + }; |
| 96 | + |
| 97 | + it("the backup, archive, and local files should all be identical as sqlite database", async () => { |
| 98 | + // they are not the same as files though so we need some care to compare them. |
| 99 | + expect(await serialize(local)).toEqual(await serialize(backup)); |
| 100 | + expect(await serialize(archive)).toEqual(await serialize(backup)); |
| 101 | + }); |
| 102 | + |
| 103 | + it("delete the local copy and open stream, the data is still available", async () => { |
| 104 | + await fs.unlink(local); |
| 105 | + |
| 106 | + s1 = stream({ |
| 107 | + client, |
| 108 | + user: { hub_id: "x" }, |
| 109 | + storage: { path: "hub/foo" }, |
| 110 | + }); |
| 111 | + const mesg = await s1.get({ key: "my-key-1" }); |
| 112 | + expect(mesg.data).toBe("one"); |
| 113 | + |
| 114 | + await s1.set({ |
| 115 | + key: "my-key-3", |
| 116 | + messageData: messageData("three"), |
| 117 | + }); |
| 118 | + |
| 119 | + s1.close(); |
| 120 | + await waitUntilClosed(); |
| 121 | + }); |
| 122 | + |
| 123 | + it("delete the archive copy and open stream, the data is still available because local is used", async () => { |
| 124 | + await fs.unlink(archive); |
| 125 | + |
| 126 | + s1 = stream({ |
| 127 | + client, |
| 128 | + user: { hub_id: "x" }, |
| 129 | + storage: { path: "hub/foo" }, |
| 130 | + }); |
| 131 | + const mesg = await s1.get({ key: "my-key-3" }); |
| 132 | + expect(mesg.data).toBe("three"); |
| 133 | + |
| 134 | + s1.close(); |
| 135 | + await waitUntilClosed(); |
| 136 | + }); |
| 137 | + |
| 138 | + it("all should identical again sqlite database", async () => { |
| 139 | + // they are not the same as files though so we need some care to compare them. |
| 140 | + expect(await serialize(local)).toEqual(await serialize(backup)); |
| 141 | + expect(await serialize(archive)).toEqual(await serialize(backup)); |
| 142 | + }); |
| 143 | + |
| 144 | + it("if both archive and local exist and local is newer, it is used", async () => { |
| 145 | + // grab copy of local |
| 146 | + const copy = local + ".copy"; |
| 147 | + await fs.copyFile(local, copy); |
| 148 | + |
| 149 | + s1 = stream({ |
| 150 | + client, |
| 151 | + user: { hub_id: "x" }, |
| 152 | + storage: { path: "hub/foo" }, |
| 153 | + }); |
| 154 | + await s1.set({ |
| 155 | + key: "my-key-4", |
| 156 | + messageData: messageData("four"), |
| 157 | + }); |
| 158 | + fs.unlink(backup); |
| 159 | + s1.close(); |
| 160 | + await wait({ |
| 161 | + until: async () => await pathExists(backup), |
| 162 | + }); |
| 163 | + |
| 164 | + // ensure the old copy of local is the newer one by making archive old |
| 165 | + await fs.copyFile(copy, local); |
| 166 | + await fs.utimes( |
| 167 | + archive, |
| 168 | + Date.now() / 1000 - 100_000, |
| 169 | + Date.now() / 1000 - 100_000, |
| 170 | + ); |
| 171 | + s1 = stream({ |
| 172 | + client, |
| 173 | + user: { hub_id: "x" }, |
| 174 | + storage: { path: "hub/foo" }, |
| 175 | + }); |
| 176 | + expect((await s1.get({ key: "my-key-4" }))?.data).toEqual(undefined); |
| 177 | + |
| 178 | + s1.close(); |
| 179 | + await waitUntilClosed(); |
| 180 | + }); |
| 181 | + |
| 182 | + it("if both archive and local exist and archive is newer, then archive is used", async () => { |
| 183 | + // grab copy of archive |
| 184 | + const copy = archive + ".copy"; |
| 185 | + await fs.copyFile(archive, copy); |
| 186 | + |
| 187 | + s1 = stream({ |
| 188 | + client, |
| 189 | + user: { hub_id: "x" }, |
| 190 | + storage: { path: "hub/foo" }, |
| 191 | + }); |
| 192 | + await s1.set({ |
| 193 | + key: "my-key-5", |
| 194 | + messageData: messageData("five"), |
| 195 | + }); |
| 196 | + s1.close(); |
| 197 | + await waitUntilClosed(); |
| 198 | + |
| 199 | + // ensure the old copy of archive is the newer one by making local old |
| 200 | + await fs.copyFile(copy, archive); |
| 201 | + await fs.utimes( |
| 202 | + local, |
| 203 | + Date.now() / 1000 - 100_000, |
| 204 | + Date.now() / 1000 - 100_000, |
| 205 | + ); |
| 206 | + s1 = stream({ |
| 207 | + client, |
| 208 | + user: { hub_id: "x" }, |
| 209 | + storage: { path: "hub/foo" }, |
| 210 | + }); |
| 211 | + expect((await s1.get({ key: "my-key-5" }))?.data).toEqual(undefined); |
| 212 | + |
| 213 | + s1.close(); |
| 214 | + await waitUntilClosed(); |
| 215 | + }); |
| 216 | + |
| 217 | + it("another check all are equal now", async () => { |
| 218 | + //console.log("checking equality"); |
| 219 | + expect(await serialize(local)).toEqual(await serialize(backup)); |
| 220 | + expect(await serialize(archive)).toEqual(await serialize(backup)); |
| 221 | + }); |
| 222 | + |
| 223 | + it("deletes local and archive but not backup -- data is NOT available", async () => { |
| 224 | + await fs.unlink(local); |
| 225 | + await fs.unlink(archive); |
| 226 | + s1 = stream({ |
| 227 | + client, |
| 228 | + user: { hub_id: "x" }, |
| 229 | + storage: { path: "hub/foo" }, |
| 230 | + }); |
| 231 | + expect((await s1.get({ key: "my-key-1" }))?.data).toEqual(undefined); |
| 232 | + }); |
| 233 | +}); |
| 234 | + |
| 235 | +async function serialize(path: string): Promise<string> { |
| 236 | + while (true) { |
| 237 | + const db = new sqlite(path); |
| 238 | + try { |
| 239 | + const x = JSON.stringify({ |
| 240 | + messages: db.prepare("select * from messages").all(), |
| 241 | + config: db.prepare("select * from config").all(), |
| 242 | + }); |
| 243 | + db.close(); |
| 244 | + return x; |
| 245 | + } catch (err) { |
| 246 | + console.log(err); |
| 247 | + } |
| 248 | + await delay(50); |
| 249 | + } |
| 250 | +} |
| 251 | + |
| 252 | +afterAll(async () => { |
| 253 | + after(); |
| 254 | +}); |
0 commit comments