Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions di/compression/VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.1.0
2 changes: 1 addition & 1 deletion di/permissions/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.0
0.2.0
17 changes: 15 additions & 2 deletions di/permissions/permissions.q
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,21 @@
/ is permitted rather than refused.

/ config coercion - a .toml tier gives strings, a .q tier gives typed values
asbool:{[x] $[-1h=type x;x;10h=abs type x;first "B"$(),x;-7h=type x;0<>x;0b]};
aslong:{[x] $[-7h=type x;x;10h=abs type x;"J"$(),x;"j"$x]};
asbool:{[x]
if[-1h=type x;:x];
if[(type x) in -4 -5 -6 -7 -8 -9h;:0<>x];
if[10h=abs type x;
s:`$lower (),x;
:$[s in `1`true;1b;
s in `0`false;0b;
'"di.permissions: cannot read \"",(string s),"\" as a boolean - use true or false"]];
'"di.permissions: cannot read a value of type ",(string type x)," as a boolean - use true or false"
};
aslong:{[x]
r:$[-7h=type x;x;10h=abs type x;"J"$(),x;"j"$x];
if[null r;'"di.permissions: cannot read \"",$[10h=abs type x;(),x;-3!x],"\" as a whole number"];
r
};

/ wildcard/superuser marker for functions and data
ALL:`$"*";
Expand Down
1 change: 1 addition & 0 deletions di/permissions/test.csv
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ true,0,0,q,`name`public`descrip`params`return~cols pm[`getapimeta][],1,,getapime
true,0,0,q,all pm[`getapimeta][][`name] in key pm,1,,every documented name is an exported function

comment,0,0,q,,1,,--- a dispatched payload q elides when stringified (di.asyncdispatch's shape) ---
fail,0,0,q,pm[`init][badmaxsizecfg[];mockdeps[]],1,,a maxsize that does not parse must error rather than refuse every result
run,0,0,q,pm[`init][cfg[1b];mockdeps[]],1,,re-init with the defaults
true,0,0,q,"runsok[`alice;elidedpayload[]]",1,,lamq's tokenizer must not signal on an elided function source
true,0,0,q,"`.gw.addserverresult~first pm[`requ][`alice;elidedpayload[]]",1,,and the payload runs - it reports the result callback
Expand Down
3 changes: 3 additions & 0 deletions di/permissions/test.q
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ badlog:{[] enlist[`log]!enlist `info`warn!({[c;m]};{[c;m]})}
cfg:{[e] (enlist`enabled)!enlist e}
cfgwith:{[k;v] (`enabled`maxsize`runmode`permissivemode!(1b;200000000;1b;0b)),(enlist k)!enlist v}

/ a maxsize that does not parse
badmaxsizecfg:{[] cfgwith[`maxsize;"2e8x"]}

/ which events got an exec owner
execevents:{[] asc exec event from reg where phase=`exec}
pcregistered:{[] 0<count select from reg where event=`.z.pc}
Expand Down
2 changes: 1 addition & 1 deletion di/torq/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.5.0
0.6.0
2 changes: 2 additions & 0 deletions di/torq/compressionconfig.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
table,minage,column,calgo,cblocksize,clevel
default,10,default,1,16,0
1 change: 1 addition & 0 deletions di/torq/proc/compression/VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.1.0
118 changes: 118 additions & 0 deletions di/torq/proc/compression/compression.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
# di.torq.proc.compression

A one-shot process type that compresses an existing hdb. It walks the partitions up to a maximum
age and rewrites every column named in a compression config csv, using the algorithm, block size
and level that csv gives it.

Ported from TorQ's `code/processes/compression.q` and `config/settings/compression.q`.

The compression itself is **not** here — it lives in `di.compression`, which was ported
separately. This module is the process wrapper: config resolution, validation, and the one-shot
run/exit lifecycle.

## Dependencies

| | |
|---|---|
`log` | injected by di.torq, required |
`di.compression` | the compression engine, `use`d directly |

## Config

Defaults ship in `di/torq/settings/compression.q`; an app overrides them per proctype or
procname, or on the command line.

| setting | default | meaning |
|---|---|---|
`csvpath` | `:appconfig/compressionconfig.csv` | the compression config csv. Resolved under `TORQXAPPHOME` — it is config, not data |
`hdbpath` | `:hdb` | hdb root to compress. Resolved under `TORQXDATAHOME`, falling back to `TORQXAPPHOME` |
`maxage` | `30` | only scan partitions this many days old or newer |
`exitonfinish` | `1b` | exit the process when the pass completes |

### Migrating from TorQ

| TorQ | here |
|---|---|
`.cmp.inputcsv` | `csvpath` — renamed to match `di.compression`'s own `compressmaxage[hdbpath;csvpath;maxage]` parameter |
`.cmp.hdbpath` | `hdbpath` |
`.cmp.maxage` | `maxage` — ships `30`, as TorQ's settings file does (its code default of 365 is overridden there) |
`.cmp.exitonfinish` | `exitonfinish` |

TorQ's settings file also switched off `.usage`, `.clients`, `.servers` and `.hb`. Here those are
ordinary settings sections, so an app turns them off in its own `compression.toml` if it wants
them off — the framework tier does not force it.

## The compression csv

Unchanged from TorQ, and read by `di.compression`, not by this module:

```
table,minage,column,calgo,cblocksize,clevel
trade,0,price,2,17,5
```

`calgo` is kdb's algorithm number (0 none, 1 ipc, 2 gzip, 3 snappy on 3.4+), `cblocksize` the
power-of-two block size 12–19, `clevel` 0–9.

## Behaviour

The pass runs from di.torq's post-init hook as `.compression.run[]`, **not** from `init`.

- `init` must not exit — `torqx.sh` owns the process lifecycle, and exiting from inside `init`
would fight it. This is the same reasoning `di.torq.proc.housekeeping` gives for declining to
port legacy's one-shot mode.
- keeping the pass in its own function leaves it re-triggerable by hand or over IPC as
`.compression.compressnow[]`, without killing the session.

Both names are published at real root names, because `use`-loading this module compiles it into a
private namespace where neither the runhook nor an IPC caller could reach them.

### `process.csv`

A batch job with no listening port:

```
host,port,proctype,procname,startwithall
localhost,0,compression,compression1,0
```

### A bad hdb path is fatal

`init` refuses a `hdbpath` that does not exist. Without that check the partition traverse simply
returns nothing and the run reports success having compressed zero columns — which reads exactly
like a correctly-configured no-op.

## Known divergence: an invalid csv does not abort the run

`di.compression.loadcsv` traps its own `checkcsv` and returns, logging an error:

```q
res:.[checkcsv;(loadedcsv;string inputcsv);{.z.m.logerr[...];:0b}];
if[res~0b;:(::)];
```

TorQ's `loadcsv` calls `checkcsv[compressioncsv]` **untrapped**, so an invalid algorithm, block
size or level throws and aborts the run.

So a misconfigured csv here is logged and skipped, and the pass continues with whatever config
was previously loaded — or none. The process still exits 0. This divergence predates this module
(it came in with `di.compression` itself) and is **not** changed here; it is recorded so that an
operator reading a successful compression log knows to check for an error line above it. The test
suite asserts the current behaviour rather than TorQ's, so changing it will fail a test
deliberately.

## Testing

```q
k4unit:use`di.k4unit
k4unit.moduletest`di.torq.proc.compression
```

The suite builds a real two-partition hdb under `/tmp`, compresses it, and asserts with `-21!`
that the configured column is compressed in both partitions while an unconfigured one is not.
It uses 200,000 rows per partition on purpose: kdb will not compress a file smaller than its
block size, so a token few rows would leave the test asserting on a no-op indistinguishable from
a failure.

`run[]` is deliberately never called by the tests — with the default `exitonfinish` it would
terminate the test process.
102 changes: 102 additions & 0 deletions di/torq/proc/compression/compression.q
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/ one-shot hdb compression process type - compresses an hdb once at startup, then exits

/ has init run?
initialised:{[] @[{.z.m.log;1b};::;{[e] 0b}]}

/ refuse an exported call before init has wired the deps and config
requireinit:{[ctx]
if[not initialised[];
'"di.torq.proc.compression: ",string[ctx],": init must be called before any other function"];
}

/ coerce a config value that may arrive typed (.q) or as text (.toml)
asbool:{[x]
if[-1h=type x;:x];
if[(type x) in -4 -5 -6 -7 -8 -9h;:0<>x];
if[10h=abs type x;
s:`$lower (),x;
:$[s in `1`true;1b;
s in `0`false;0b;
'"di.torq.proc.compression: cannot read \"",(string s),"\" as a boolean - use true or false"]];
'"di.torq.proc.compression: cannot read a value of type ",(string type x)," as a boolean - use true or false"
}

aslong:{[x]
r:$[-7h=type x;x;10h=abs type x;"J"$(),x;"j"$x];
if[null r;'"di.torq.proc.compression: cannot read \"",$[10h=abs type x;(),x;-3!x],"\" as a whole number"];
r
}

/ code and config under TORQXAPPHOME, runtime data under TORQXDATAHOME
apphome:{getenv[`TORQXAPPHOME]}
datahome:{$[count h:getenv[`TORQXDATAHOME];h;getenv[`TORQXAPPHOME]]}

/ resolve a possibly-relative path setting to an absolute path string under base
resolvedir:{[base;dir]
dir:$[10h=abs type dir;(),dir;string dir];
dir:$[(0<count dir) and ":"=first dir;1_dir;dir];
$[dir like "/*";dir;base,"/",dir]
}

/ the app's compression csv if it exists, else the one this framework ships
resolvecsv:{[config]
p:hsym `$resolvedir[apphome[];$[`csvpath in key config;config`csvpath;"appconfig/compressionconfig.csv"]];
$[count key p;p;hsym `$getenv[`TORQXHOME],"/di/torq/compressionconfig.csv"]
}

/ wrap the error logger so a pass's errors are counted
counterrors:{[logdep]
logdep,enlist[`error]!enlist {[f;c;m] .z.m.errors+:1; f[c;m]}[logdep`error]
}

/ run one compression pass - re-triggerable, does not exit
compressnow:{[]
requireinit[`compressnow];
.z.m.errors:0;
.z.m.log[`info][`compression;"compressing ",(string .z.m.hdbpath)," using ",string .z.m.csvpath];
(.z.m.cmp`compressmaxage)[.z.m.hdbpath;.z.m.csvpath;.z.m.maxage];
.z.m.log[`info][`compression;"finished compression"];
}

/ di.torq's post-init hook - compress, then exit; non-zero if the pass logged errors
run:{[]
compressnow[];
if[not .z.m.exitonfinish;:()];
if[.z.m.errors>0;
.z.m.log[`warn][`compression;"exiting 1 - ",(string .z.m.errors)," error(s) during compression"];
exit 1];
.z.m.log[`info][`compression;"exitonfinish set, exiting"];
exit 0;
}

/ di.compression's post-compression summary table
getstatstab:{[] requireinit[`getstatstab]; (.z.m.cmp`getstatstab)[]}

/ how many errors the last pass logged
errorcount:{[] requireinit[`errorcount]; .z.m.errors}

init:{[config;deps]
/ resolve and validate before setting any module state
if[not `log in key deps;'"di.torq.proc.compression: log dependency is required - see di.util.log"];
lg:deps`log;
hp:hsym `$resolvedir[datahome[];$[`hdbpath in key config;config`hdbpath;"hdb"]];
cp:resolvecsv config;
/ defaults - must match the shipped settings file
ma:$[`maxage in key config;aslong config`maxage;30];
eof:$[`exitonfinish in key config;asbool config`exitonfinish;1b];
/ refuse a missing hdb path
if[not count key hp;lg[`error][`compression;e:"invalid hdb path ",string hp];'e];
.z.m.errors:0;
.z.m.log:counterrors lg;
.z.m.hdbpath:hp;
.z.m.csvpath:cp;
.z.m.maxage:ma;
.z.m.exitonfinish:eof;
.z.m.cmp:use`di.compression;
(.z.m.cmp`init)[enlist[`log]!enlist .z.m.log];
/ publish at root names for the runhook and IPC callers
set[`.compression.run;run];
set[`.compression.compressnow;compressnow];
.z.m.log[`info][`init;"initialised, hdbpath=",(string hp),", csvpath=",(string cp),
", maxage=",(string ma),", exitonfinish=",string eof];
}
3 changes: 3 additions & 0 deletions di/torq/proc/compression/deps.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Peer modules di.torq.proc.compression `use`s (see di/torq/depcheck/depcheck.md).
[dependencies]
"di.compression" = "0.1.0"
5 changes: 5 additions & 0 deletions di/torq/proc/compression/init.q
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
\l ::compression.q

version:first read0`:::VERSION

export:([init;compressnow;getstatstab;errorcount;version])
49 changes: 49 additions & 0 deletions di/torq/proc/compression/test.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
action,ms,bytes,lang,code,repeat,minver,comment
before,0,0,q,cmp:use`di.torq.proc.compression,1,,load module
before,0,0,q,system "l di/torq/proc/compression/test.q",1,,load mock deps and fixture helpers
before,0,0,q,setupfixture[],1,,build the fixture hdb and its compression csv

fail,0,0,q,cmp[`init][basecfg[];()!()],1,,init without a log dependency must error
fail,0,0,q,cmp[`compressnow][],1,,compressnow before init must error
fail,0,0,q,cmp[`init][badhdbcfg[];mockdeps[]],1,,init on a missing hdb path must error

true,0,0,q,`csvpath`hdbpath`maxage`exitonfinish~key shippeddefaults[],1,,the settings file declares every tunable
true,0,0,q,1b~(shippeddefaults[])`exitonfinish,1,,shipped exitonfinish default
true,0,0,q,30~(shippeddefaults[])`maxage,1,,shipped maxage default matches TorQ's config/settings/compression.q

run,0,0,q,resetcalls[],1,,clear the captured log
run,0,0,q,cmp[`init][nodefaultscfg[];mockdeps[]],1,,init with only the required paths so the code defaults apply
true,0,0,q,initlog[] like "*maxage=30*",1,,code maxage default agrees with the settings file
true,0,0,q,initlog[] like "*exitonfinish=1",1,,code exitonfinish default agrees with the settings file

run,0,0,q,cmp[`init][basecfg[];mockdeps[]],1,,init with the full fixture config
true,0,0,q,not compressed[PARTS 0;`price],1,,the fixture starts uncompressed
run,0,0,q,cmp[`compressnow][],1,,run one compression pass
true,0,0,q,compressed[PARTS 0;`price],1,,the configured column is compressed in the first partition
true,0,0,q,compressed[PARTS 1;`price],1,,and in the second
true,0,0,q,not compressed[PARTS 0;`size],1,,a column the csv does not name is left alone
true,0,0,q,not compressed[OLD;`price],1,,a partition older than maxage is left alone

true,0,0,q,100h=type get `.compression.run,1,,run is published at a root name for di.torq's runhook
true,0,0,q,100h=type get `.compression.compressnow,1,,compressnow is published at a root name for IPC callers

run,0,0,q,teardownfixture[],1,,rebuild the fixture so the toml run has uncompressed files to work on
run,0,0,q,setupfixture[],1,,
run,0,0,q,cmp[`init][tomlcfg[];mockdeps[]],1,,init accepts the string values a .toml settings file delivers
run,0,0,q,cmp[`compressnow][],1,,and compresses with them
true,0,0,q,compressed[PARTS 0;`price],1,,the string-valued maxage was coerced and the pass ran
fail,0,0,q,cmp[`init][badboolcfg[];mockdeps[]],1,,a boolean setting that is neither true/false nor 1/0 must error rather than silently read as false
fail,0,0,q,cmp[`init][symboolcfg[];mockdeps[]],1,,a symbol must error too - it used to fall through the type guard and read as false
fail,0,0,q,cmp[`init][badlongcfg[];mockdeps[]],1,,a maxage that does not parse must error - a 0N maxage compressed nothing and exited 0

run,0,0,q,cmp[`init][nocsvcfg[];mockdeps[]],1,,an app with no compression csv falls back to the one the framework ships
true,0,0,q,initlog[] like "*di/torq/compressionconfig.csv*",1,,and the init log names it

run,0,0,q,writebadalgocsv[],1,,swap in a csv naming an unsupported algorithm
run,0,0,q,cmp[`init][basecfg[];mockdeps[]],1,,point back at the fixture csv - the previous row inited against the framework one
run,0,0,q,resetcalls[],1,,clear the captured log
run,0,0,q,cmp[`compressnow][],1,,an invalid algorithm is logged and skipped rather than thrown - see compression.md
true,0,0,q,0<count select from calls where lvl=`error,1,,the invalid config is reported as an error
true,0,0,q,0<cmp[`errorcount][],1,,and counted - run exits non-zero so a scheduler does not read it as success

after,0,0,q,teardownfixture[],1,,remove the fixture hdb
Loading