Skip to content

Commit c211a2a

Browse files
authored
Merge pull request #161 from unisoncomputing/task-validate-all-components
Background Task to validate all components and reject invalid components from sync
2 parents 1418ec7 + 5bbc501 commit c211a2a

File tree

5 files changed

+46
-22
lines changed

5 files changed

+46
-22
lines changed

Makefile

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ $(target_dir):
3030
$(exe): $(shell find . unison -type f -name '*.hs') $(shell find . unison -type f -name '*.yaml')
3131
@echo $(exe)
3232
@echo $@
33-
stack build $(STACK_FLAGS) share-api:share-api
33+
stack build $(STACK_FLAGS)
3434

3535
$(installed_share): $(exe) $(target_dir)
3636
cp $(exe) $(installed_share)
@@ -109,3 +109,24 @@ transcripts: $(installed_share)
109109
kill $$SERVER_PID 2>/dev/null || true; \
110110
)
111111
@echo "Transcripts complete!";
112+
113+
task-runner: $(installed_share)
114+
@echo "Taking down any existing docker dependencies"
115+
@docker compose -f docker/docker-compose.base.yml down || true
116+
@trap 'docker compose -f docker/docker-compose.base.yml down' EXIT INT TERM
117+
@echo "Booting up task docker dependencies..."
118+
docker compose -f docker/docker-compose.base.yml up --remove-orphans --detach
119+
@while ! ( pg_isready --host localhost -U postgres -p 5432 >/dev/null 2>&1 && redis-cli -p 6379 ping >/dev/null 2>&1 && VAULT_ADDR=http://localhost:8200 vault status >/dev/null 2>&1 ) do \
120+
sleep 1; \
121+
done;
122+
./transcripts/configure_transcript_database.zsh
123+
@echo "Booting up share";
124+
( . ./local.env ; \
125+
$(exe) & \
126+
SERVER_PID=$$!; \
127+
trap "kill $$SERVER_PID 2>/dev/null || true" EXIT INT TERM; \
128+
echo "Running task"; \
129+
stack exec share-task-runner; \
130+
kill $$SERVER_PID 2>/dev/null || true; \
131+
)
132+
@echo "Task complete!";

docker/share-task-runner-entrypoint.sh

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,6 @@
22

33
set -ex
44

5-
echo SHARE_REDIS: "$SHARE_REDIS"
6-
7-
if [ -n "$NOMAD_PORT_enlil_http" ]; then
8-
export SHARE_SERVER_PORT="$NOMAD_PORT_enlil_http"
9-
fi
10-
115
export SHARE_IP=0.0.0.0
126

137
exec 2>&1

share-api/src/Share/Web/UCM/Sync/Impl.hs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ import Share.Web.Errors
5252
import Share.Web.UCM.Sync.HashJWT qualified as HashJWT
5353
import Share.Web.UCM.Sync.Types (EntityBunch (..), RepoInfoKind (..), entityKind)
5454
import U.Codebase.Causal qualified as Causal
55+
import U.Codebase.Sqlite.HashHandle qualified as HH
5556
import U.Codebase.Sqlite.Orphans ()
5657
import Unison.Codebase.Path qualified as Path
5758
import Unison.Hash32 (Hash32)
@@ -259,7 +260,11 @@ insertEntitiesToCodebase codebase entities = do
259260
mayErrs <- PG.transactionUnsafeIO $ batchValidateEntities maxParallelismPerUploadRequest isComponentHashMismatchAllowedIO isCausalHashMismatchAllowedIO unsavedEntities
260261
case mayErrs of
261262
Nothing -> pure ()
262-
Just (err :| _errs) -> throwError err
263+
Just (err :| _errs) ->
264+
case err of
265+
Right e -> throwError e
266+
Left (HH.IncompleteElementOrderingError (ComponentHash hash)) ->
267+
throwError $ Sync.InvalidByteEncoding (Hash32.fromHash hash) Sync.TermComponentType "Incomplete element ordering in term components"
263268
SyncQ.saveTempEntities codebase unsavedEntities
264269
let hashesNowInTemp = Set.fromList (fst <$> Foldable.toList unsavedEntities) <> (Set.fromList . Foldable.toList $ hashesAlreadyInTemp)
265270
pure hashesNowInTemp
@@ -393,7 +398,7 @@ batchValidateEntities ::
393398
(ComponentHash -> ComponentHash -> IO Bool) ->
394399
(CausalHash -> CausalHash -> IO Bool) ->
395400
f (Hash32, Sync.Entity Text Hash32 Hash32) ->
396-
IO (Maybe (NonEmpty (Sync.EntityValidationError)))
401+
IO (Maybe (NonEmpty (Either HH.HashingFailure Sync.EntityValidationError)))
397402
batchValidateEntities maxParallelism checkIfComponentHashMismatchIsAllowed checkIfCausalHashMismatchIsAllowed entities = do
398403
errs <- UnliftIO.pooledForConcurrentlyN maxParallelism entities \(hash, entity) ->
399404
validateEntity checkIfComponentHashMismatchIsAllowed checkIfCausalHashMismatchIsAllowed hash entity
@@ -405,16 +410,16 @@ validateEntity ::
405410
(CausalHash -> CausalHash -> m Bool) ->
406411
Hash32 ->
407412
Share.Entity Text Hash32 Hash32 ->
408-
m (Maybe Sync.EntityValidationError)
413+
m (Maybe (Either HH.HashingFailure Sync.EntityValidationError))
409414
validateEntity checkIfComponentHashMismatchIsAllowed checkIfCausalHashMismatchIsAllowed hash entity = do
410415
case (Sync.validateEntity hash entity) of
411-
Just err@(Sync.EntityHashMismatch Sync.TermComponentType (Sync.HashMismatchForEntity {supplied = expectedHash, computed = actualHash})) ->
416+
Just (Right (err@(Sync.EntityHashMismatch Sync.TermComponentType (Sync.HashMismatchForEntity {supplied = expectedHash, computed = actualHash})))) ->
412417
checkIfComponentHashMismatchIsAllowed (ComponentHash . Hash32.toHash $ expectedHash) (ComponentHash . Hash32.toHash $ actualHash) >>= \case
413-
False -> pure (Just err)
418+
False -> pure (Just $ Right err)
414419
True -> pure Nothing
415-
Just err@(Sync.EntityHashMismatch Sync.CausalType (Sync.HashMismatchForEntity {supplied = expectedHash, computed = actualHash})) ->
420+
Just (Right (err@(Sync.EntityHashMismatch Sync.CausalType (Sync.HashMismatchForEntity {supplied = expectedHash, computed = actualHash})))) ->
416421
checkIfCausalHashMismatchIsAllowed (CausalHash . Hash32.toHash $ expectedHash) (CausalHash . Hash32.toHash $ actualHash) >>= \case
417-
False -> pure (Just err)
422+
False -> pure (Just $ Right err)
418423
True -> pure Nothing
419424
Just err ->
420425
-- This shouldn't happen unless the ucm client is buggy or malicious

share-task-runner/src/Share/Tasks/AmbiguousComponentCheck.hs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import Share.Postgres.Cursors qualified as PG
88
import Share.Prelude
99
import Share.Utils.Logging (Loggable (..))
1010
import Share.Utils.Logging qualified as Logging
11+
import U.Codebase.Sqlite.HashHandle qualified as HH
1112
import U.Codebase.Sqlite.TempEntity
1213
import Unison.Hash32
1314
import Unison.Sync.EntityValidation qualified as EV
@@ -17,7 +18,7 @@ import Unison.Util.Servant.CBOR qualified as CBOR
1718

1819
data AmbiguousComponentCheckError
1920
= TaskAmbiguousComponentCheckError Hash32
20-
| TaskEntityValidationError Hash32 Sync.EntityValidationError
21+
| TaskEntityValidationError Hash32 (Either HH.HashingFailure Sync.EntityValidationError)
2122
| TaskEntityDecodingError Hash32 CBOR.DeserialiseFailure
2223
deriving (Show, Eq)
2324

@@ -51,12 +52,15 @@ run = withWorkerName "ambiguous-component-task" do
5152
PG.newRowCursor @(CBORBytes TempEntity, Hash32)
5253
"component_cursor"
5354
[PG.sql|
54-
(SELECT DISTINCT ON (t.component_hash_id) bytes.bytes, ch.base32
55-
FROM terms t
56-
JOIN serialized_components sc ON t.component_hash_id = sc.component_hash_id
57-
JOIN bytes ON sc.bytes_id = bytes.id
58-
JOIN component_hashes ch ON t.component_hash_id = ch.id
59-
)
55+
WITH component_hash_ids(component_hash_id) AS (
56+
SELECT DISTINCT component_hash_id
57+
FROM terms t
58+
WHERE t.component_index = 1
59+
) SELECT DISTINCT ON (bytes.id) bytes.bytes, ch.base32
60+
FROM component_hash_ids chi
61+
JOIN serialized_components sc ON chi.component_hash_id = sc.component_hash_id
62+
JOIN bytes ON sc.bytes_id = bytes.id
63+
JOIN component_hashes ch ON chi.component_hash_id = ch.id
6064
|]
6165
PG.foldBatched cursor 100 \rows -> do
6266
rows

unison

Submodule unison updated 32 files

0 commit comments

Comments
 (0)