Worker Mode
Description
This unit describes the functionality for the system's worker mode. When running we expect \(n\) instances of the worker to run simultaneously.
Public Interfaces
Faktory Job
The Faktory worker interface serves as the primary entry point for worker mode. The interface retrieves and processes tangle generation jobs distributed by Faktory.
State Machine
stateDiagram-v2
state "Await job" as rj
state "Create instance of worker class" as grp
state "Process job" as gsp
state error <<choice>>
[*]--> rj
rj --> grp
grp --> gsp
gsp --> error
error --> rj : Else
error --> [*]: Error
Private interface
Class Worker
The worker class describes the data and methods needed for an atomic generation job.
Process Job
The process job method contains the core logic of the worker class. The method connects to the MongoDB tangle collection and retrieves the pages pointed to by the job data. The method then calls the low-level grafting code to generate the new tangles from the pages.
State Machine
stateDiagram-v2
state "Retrieve job" as rj
state "Get rootstock page" as grp
state "Get scion page" as gsp
state "Graft pages" as gp
state "Store new tangles" as snt
state "Report job results" as rjr
[*]--> rj
rj --> grp
grp --> gsp
gsp --> gp
gp --> snt
snt --> rjr
rjr --> [*]
Unit test description
Process Job
Positive Tests
Valid job
A valid job and collection is configured. Job processing is started. Correct output tangles are written.
Inputs:
- Mocked MongoDB collection
- Page size set to two
Expected Output:
- Tangles correctly generated
- Each tangle is seen in a page.
Negative tests
Invalid collection
An invalid collection is configured. Inputs:
- Mocked invalid MongoDB collection
Expected Output:
- Exception is thrown
Invalid Index
A page index is found in the job but not found in the collection.
Inputs:
- Mocked valid MongoDB collection
- Invalid index
Expected Output:
- Exception is thrown
Implementation
runner.fworker.fworker
The faktory worker core functionality.
Worker
Worker(arbor_col, rt_idx, rt_tcn, sci_idx, sci_tcn, page_size)
Class defines an atomic worker.
Attributes:
| Name | Type | Description |
|---|---|---|
_arbor_col |
Collection
|
Mongodb collection of arborescent tangles. |
_rt_idx |
str
|
ID of the start of the rootstock page. |
_rt_tcn |
int
|
TCN of the rootstocks. |
_sci_idx |
str
|
ID of the start of the scion page. |
_sci_tcn |
int
|
TCN of the scions. |
_tang_list |
dict[str, dict]
|
Dictionary of the generated tangles and attributes. |
_notes |
set[str]
|
Set of generated tangles, used for generating non-good trees. |
Init the worker.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
arbor_col
|
Collection
|
The arborescent tangle collection. |
required |
rt_idx
|
str
|
ID of the start of the rootstock page. |
required |
rt_tcn
|
int
|
TCN of the rootstocks. |
required |
sci_idx
|
str
|
ID of the start of the scion page. |
required |
sci_tcn
|
int
|
TCN of the scions. |
required |
page_size
|
int
|
The length of a page for the job. |
required |
Source code in runner/fworker/fworker.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | |
_batch_write
_batch_write()
Batch write the generated data to the Mongodb collection.
Source code in runner/fworker/fworker.py
61 62 63 64 65 66 67 68 69 70 71 72 73 74 | |
process
process()
Process the job.
Source code in runner/fworker/fworker.py
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | |
faktory_job
faktory_job(rt_idx, rt_tcn, sci_idx, sci_tcn, page_size)
Pyfaktory worker callback for generating RLITT.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rt_idx
|
str
|
ID of the start of the rootstock page. |
required |
rt_tcn
|
int
|
TCN of the rootstocks. |
required |
sci_idx
|
str
|
ID of the start of the scion page. |
required |
sci_tcn
|
int
|
TCN of the scions. |
required |
page_size
|
int
|
The size of the page of tangles to retrieve. |
required |
Source code in runner/fworker/fworker.py
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | |