Configure a ZFS volume for Time Machine on NAS

Preparation

A ZFS pool should surely pre-exist there to allow for the operations introduced later.

  • Key point: Why not split, or shrink existing volumes for a Time Machine volume?

    Not viable. A ZFS partition always tries to occupy as much space as possible, so does ZFS volumes under it, sharing the entire space of it. To reserving some space is exactly what the quota property is designed for.

    In addition, you can use the reservation property to guarantee that a specified amount of disk space is available to a file system. Both properties apply to the dataset on which they are set and all descendents of that dataset.

    More…

  • Creating a new ZFS filesystem

    zfs create -o compression=lz4 pool/filesystem

    where the purpose of an optional argument -o compression=lz4 is to turn on the compression for this dataset.

Read more

Non Built-in Data Structures And Algorithms in Python3

Unordered MultiSet

1
2
3
4
5
6
7
8
9
10
11
12
13
class MultiSet:
def __init__(self):
self.data = dict()

def add(self, val):
self.data[val] = 1 + (self.data[val] if val in self.data else 0)

def remove(self, val):
if not val in self.data:
return
self.data[val] -= 1
if self.data[val] == 0:
del self.data[val]

Fenwick Tree / Binary-indexed Tree

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class BIT:
def __init__(self, size):
self.size = size
self.data = [0] * size

def add(self, pos, val):
while pos < self.size:
self.data[pos] += val
pos += pos & (-pos)

def query(self, pos):
res = 0
while pos > 0:
res += self.data[pos]
pos &= pos - 1
return res
Read more

Universal Code Competition Template in Python3 (editing) [2022]

Number Theory

Quick power and modulo

To calculate $ n^m % mod $:

Division and modulo

DO NOT dividing an integer by another integer with respect to the modulus $mod$.
To calculate $ n/m%mod $ correctly ($ mod$ is a prime number thus $ φ(mod)=mod-1 $).
Use Eular function or modular multiplicative inversion.

Euler function $ φ(n) $

Quick greatest common divisor

1
2
3
4
5
6
7
8
9
10
11
12
13
def kgcd(a, b):
if a == 0:
return b
if b == 0:
return a
if (a & 1) == 0 and (b & 1) == 0:
return kgcd(a >> 1, b >> 1) << 1
elif (b & 1) == 0:
return kgcd(a, b >> 1)
elif (a & 1) == 0:
return kgcd(a >> 1, b)
else:
return kgcd(abs(a - b), min(a, b))
Read more

How to backup LinkedIn saved posts manually

What?

To export account data from LinkedIn website can be of a lot of muddle and hassle, but the result may not be yet decent for the missing saved posts.

Let’s do this in a manual automated way by scripting.

Solution

Code snippet for saving data as a downloadable JSON file (source):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
(function(console){
console.save = function(data, filename){

if(!data) {
console.error('Console.save: No data')
return;
}

if(!filename) filename = 'console.json'

if(typeof data === "object"){
data = JSON.stringify(data, undefined, 4)
}

var blob = new Blob([data], {type: 'text/json'}),
e = document.createEvent('MouseEvents'),
a = document.createElement('a')

a.download = filename
a.href = window.URL.createObjectURL(blob)
a.dataset.downloadurl = ['text/json', a.download, a.href].join(':')
e.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null)
a.dispatchEvent(e)
}
})(console)

This line looks weird, just an ad-hoc cure when the error of undefined $ occurs with the arrow function operating on the jQuery selector. The cause of error still remains a mystery…

Read more

Apple M1 Development Quick-start

  • Command Line Tools
1
2
3
==> Downloading and installing Homebrew...
xcrun: error: unable to load libxcrun (dlopen(/Library/Developer/CommandLineTools/usr/lib/libxcrun.dylib, 0x0005): could not use '/Library/Developer/CommandLineTools/usr/lib/libxcrun.dylib' because it is not a compatible arch).
Failed during: git init -q

To get rid of the initial git error, install a complete version of Command Line Tools from Apple Developer site.

Validate Command Line Tools:

sudo xcode-select -p

  • Homebrew

/bin/bash -c “$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)”

  • Curl & wget

brew install curl wget

If you need to have curl first in your PATH, run:

echo ‘export PATH=”/opt/homebrew/opt/curl/bin:$PATH”‘ >> ~/.zshrc

For compilers to find curl you may need to set:

export LDFLAGS=”-L/opt/homebrew/opt/curl/lib”
export CPPFLAGS=”-I/opt/homebrew/opt/curl/include”

  • Oh-my-zsh

Extend the default zsh shell with rich sources of plugins and themes.

  • NVM

You should create NVM’s working directory if it doesn’t exist:

mkdir ~/.nvm

Add the following to ~/.zshrc or your desired shell
configuration file:

1
2
3
export NVM_DIR="$HOME/.nvm"
[ -s "/opt/homebrew/opt/nvm/nvm.sh" ] && . "/opt/homebrew/opt/nvm/nvm.sh" # This loads nvm
[ -s "/opt/homebrew/opt/nvm/etc/bash_completion.d/nvm" ] && . "/opt/homebrew/opt/nvm/etc/bash_completion.d/nvm" # This loads nvm bash_completion

Soft Router Solution on Ubuntu Server 20.04

This solution is based on the built-in systemd-networkd, isc-dhcp-server and hostapd; conflicted with dnsmasq.

It is using the built-in IP forwarding feature against bridging. See also routing vs bridging

Prerequisites

  • Traffic and firewall management
1
2
sudo ufw enable
sudo ufw status
  • DHCP server
1
2
sudo service isc-dhcp-server start
sudo service isc-dhcp-server status
  • WiFi hotspot
1
2
3
sudo systemctl unmask hostapd
sudo systemctl enable hostapd
sudo systemctl status hostapd
Read more

10Gbps+ Ethernet Essentials

Prerequisites

MLNX_OFED_LINUX-5.1 no longer supports lowest-end Mellanox ConnectX3 series; using MLNX_OFED_LINUX-5.0 instead.

sudo mount [MLNX…_ubuntu20.04_x86_64.iso] /mnt/cdrom -o loop
./mlnxofedinstall

Start configuration

sudo mst start
sudo mst status

Details

ibv_devinfo

mlxconfig -d [device_identifier] query
mlxconfig -d [device_identifier] set LINK_TYPE_P1=2

sudo systemctl start opensmd

See also more.

System Design Memorandum on Priority Topics

TL;DR

Cache is to reduce latency in the system.

Cache prefers immutable or static data.
Cache can be stale when not updated. Ask, do we care that much?
Write Policy: Write through cache vs write back cache
Eviction policy: LRU, FIFO, LFU

How to sync between Cache and DB?

It is more complicated when syncing Cache replicas & DB replicas and maintain consistency & high throughput.

Get() follows cache-aside strategy ; update() follows write through strategy;

delete() request both operations into caches and DBs; upon DB changed, trigger some asynchronized threads to wait a certain while and notify the cache again to evict stale data that happened to be brought by concurrent reading from DB into cache (proven and incorporated by Facebook)

Hashing function basically transforms arbitrary pieces of data into fixed size values (typ. Integers)

Consistent hashing

Consistent hashing maximizes the cache hits when adding and removing cache nodes; minimizes key re-distribution; mitigate hotkey problem

Read more