Skip to content

Commit

Permalink
disallow versions with same txn id (#479)
Browse files Browse the repository at this point in the history
* disallow versions with same txn id

* logging tweak
  • Loading branch information
vilterp committed Jun 9, 2024
1 parent 20991cf commit 3b0e4e2
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 27 deletions.
4 changes: 2 additions & 2 deletions apps/actors/systems/kvSync/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
} from "./types";
import * as effects from "../../effects";
import { mapObj, randStep } from "../../../../util/util";
import { runMutation } from "./mutations/run";
import { addNewVersion, runMutation } from "./mutations/run";
import { InterpreterState } from "./mutations/builtins";

export type QueryStatus = "Loading" | "Online";
Expand Down Expand Up @@ -123,7 +123,7 @@ function processLiveQueryUpdate(
const key = update.key;
switch (update.type) {
case "Updated":
newData[key] = [...(newData[key] || []), update.value];
newData[key] = addNewVersion(newData, key, update.value);
break;
case "Deleted":
delete newData[key]; // TODO: tombstone?
Expand Down
5 changes: 4 additions & 1 deletion apps/actors/systems/kvSync/examples/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,10 @@ function SendBox(props: { threadID: string; client: Client }) {
if (status === "Online") {
props.client.runMutation("markRead", [props.threadID, latestSeqNo]);
} else {
console.log("not marking read because query status is", status);
console.warn(
"SendBox: not marking read because query status is",
status
);
}
}}
/>
Expand Down
43 changes: 20 additions & 23 deletions apps/actors/systems/kvSync/examples/common/kvInspector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Table } from "./table";
import { VersionedValue } from "../../types";
import { getVisibleValue } from "../../mutations/common";
import { TransactionState, isTxnVisible } from "../../client";
import { intersperse, reversed } from "../../../../../../util/util";
import { reversed } from "../../../../../../util/util";

export function KVInspector(props: {
client: Client;
Expand Down Expand Up @@ -34,28 +34,25 @@ export function KVInspector(props: {
{
name: "Statuses",
render: ([key, vvs]) =>
intersperse(
<></>,
reversed(vvs).map((vv) => (
// TODO: clicking on this should show the txn trace
<span
title={vv.transactionID}
style={{
cursor: "pointer",
textDecoration:
vv.transactionID === props.selectedTxnID
? "underline"
: "none",
}}
key={vv.transactionID}
onClick={() => props.onSelectTxn(vv.transactionID)}
>
{iconForState(
props.client.state.transactions[vv.transactionID].state
)}
</span>
))
),
reversed(vvs).map((vv) => (
// TODO: clicking on this should show the txn trace
<span
title={vv.transactionID}
style={{
cursor: "pointer",
textDecoration:
vv.transactionID === props.selectedTxnID
? "underline"
: "none",
}}
key={vv.transactionID}
onClick={() => props.onSelectTxn(vv.transactionID)}
>
{iconForState(
props.client.state.transactions[vv.transactionID].state
)}
</span>
)),
},
]}
/>
Expand Down
17 changes: 16 additions & 1 deletion apps/actors/systems/kvSync/mutations/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ function doWrite(
const newVersionedValue: VersionedValue = { transactionID, value };
const newKVData: KVData = {
...kvData,
[key]: [...(kvData[key] || []), newVersionedValue],
[key]: addNewVersion(kvData, key, newVersionedValue),
};
const oldValue = getVisibleValue(isTxnCommitted, kvData, key);
if (kvData[key]) {
Expand All @@ -346,3 +346,18 @@ function doWrite(
},
];
}

export function addNewVersion(
kvData: KVData,
key: string,
newVersion: VersionedValue
) {
const versions = kvData[key] || [];
// Check if the transactionID is already in the list
// This can result from overlapping live queries
// TODO: this is O(n) and could be O(1)
if (versions.some((v) => v.transactionID === newVersion.transactionID)) {
return versions;
}
return [...versions, newVersion];
}

0 comments on commit 3b0e4e2

Please sign in to comment.