Cassandra 叢集狀態的計算機

網路上看到的一個Cassandra的簡易計算機,作者還有的提供原始碼在Github 上,所以如果有需要的話也可以自己fork一份自己架在自己的網頁上。

這個計算機的功能也很簡單,主要是提供使用者可以快速顯示在不同的cluster狀態下,根據keyspace的read/write 設定對於叢集的影響,像是:

  • 可以容忍多少個節點失效而不遺失資料。
  • 每個節點的資料分佈比例。
  • 每次寫入會連接到的節點數量。
  • 每次讀取時會連接到的節點數量。

reference:

  • https://github.com/jalkanen/cassandracalculator
  • https://www.ecyrd.com/cassandracalculator/

使用sshuttle來連接不同環境下的機器

最近由於專案快上線了,所以頻繁的連接在不同的環境下的機器,或是監看機器的使用狀態;在這樣的使用情境下,之前有試著使用Socks5 + SwitchyOmega 的組合來連接,這樣的組合在只用來連接內網的dashboard時是蠻堪用的,但如果其它類型的連線就無法了。

每當要使用像是TCP連線到內網的機器(像是連資料庫之類的),就都要在先連一次VPN,然後才再連線;但連線完VPN以後,又要把SwitchyOmega的設定關閉,不然VPN +Socks5 + SwitchyOmega 的組合無法讓我連到特定的Dashboard…
這其實蠻惱人的,尤其是頻繁的切換使用VPN網路時,或是在開會過程中,又需要連到特定環境下的機器…

通常這問題大概可以使用幾種方式克服:

VPN – Partial Routing

若是使用OpenVPN的話可以在導入Profile時加入以下設定來讓client端可以自訂路由的規則。

route-nopull
route X.X.Y.Z 255.255.255.255

這邊的範例是只有往x.x.y.z 時才會經由vpn。

但這個方案的缺點是,同時間也只能連到一台OpenVPN 伺服器,所以代表同時間也只能連到同一個環境下的機器。

但優點就是所有TCP/UDP的封包都可以經由路由連接到那個環境下的機器。

SSH Tunnel Proxy

其實ssh 已經提供了一個方便的解決方案了,即是使用-L 這個flag,讓我們可以透過中介的jump/proxy server,讓我們的本地主機與遠端主機建立一個虛擬通道。

ssh -NL 5432:staging-postgres.your-domain.com user@jumper-server.your-domain.com

(使用-f 可以讓這個tunnel 在背景下執行。)

透過上面的指令,我們就可以執行下面的psql 指令來連接staging postgres這個資料庫。

psql -U postgres -d postgres -h 127.0.0.1 -p 5432

這個方案的缺點就是,如果有多台主機需要連的話,可能要設定多個Tunnel Proxy。

sshuttle

在Stackoverflow 上剛好看到了這篇文章,其中回應有提到了sshuttle 這個開源的專案可以透過ssh tunnel導流所有TCP的流量到遠端的jump server,也就是類似TCP Proxy的功能。

試了以後還蠻方便的,它底層的原理即是使用了ssh tunnel proxy,所以也是需要一台中介的ssh jump/proxy server。除此之外,它可以讓使用者用根據網段來轉送流量,所以基本可以解決原本在ssh tunnel proxy的一些不方便之處。

安裝

# For Mac
brew install sshuttle

# For ubuntu
sudo apt-get install sshuttle

使用上的基本指令

sshuttle -vr user@jumper-server.your-domain.com 10.0.0.0/8

上面這指令即是把所有往10.0.0.0/8的TCP流量都自動轉往jumper-server.your-domain.com上送…

Reference:

如何提升不同程式語言的技能

在Hacker Daily News上看到的一篇文章,作者提到了,當他發了一個PR到某個專案上時,專業的維護者的一些review 評論對他來說非常受用,也讓他了解到一些程式語言上 的特性與knowhow。

透過這個經驗,作者也發現了,當他去翻一些開源專案上的merged/closed PRs時,也可以發現類似的撰寫建議;所以他歸納這可以是一個通用法則,並且可以用在各個程式領域上:

I learned so much from reading the comments and concerns on my PRs. But it doesn’t have to be my PR. It can be anybody’s.

How To Rapidly Improve At Any Programming Language (cbui.dev)

簡而言之,在一個成熟且大型的開源專案上,開發與維護者通常都會是非常聰明且有知識的開發者,所以透過學習這些PRs 的建議與評論可以挖掘到許多無價的經驗。

因此作者也給了一些如何開始的建議:

1. Every morning, take your favorite open source library or one from a language you’re learning, go to the closed PRs on Github and start reading them from the beginning. Just a few a morning for warmup while you drink your morning coffee and catch up on email.

2. When you want to level up, start reading the diff, and review the code and changes yourself before reading the comments.

3. Finally, when you start feeling more confident, start leaving those comments on new PRs so that the maintainer doesn’t have to. You’re starting to contribute to open source!

How To Rapidly Improve At Any Programming Language (cbui.dev)

原文可以參考這裡:

How To Rapidly Improve At Any Programming Language (cbui.dev)

與C/C++整合的語言 – Zig

Hacker News上看到關於一個新的程式語言 – Zig, 從語言的哲學來看,它並不是要完全取代C/C++,相對的它是想要完善C/C++的生態與解決C/C++的痛點。

從生態來看,要在市場上找到很熟悉C/C++的開發者其實是困難的,相較於市場上找到會php/python/javascript 等程式語言的開發者而言;但問題是,目前幾乎所有重要軟體的基石都是使用C/C++來撰寫的。

從痛點來看,常常要編譯C/C++的程式時,可能會需要安裝許多toolchain 與dpendency,且依賴版本的管理上也不像其它語言那麼的平易近人,所以也造成要維護些C/C++專案的門檻也相對高。

所以Zig在這邊找到了它的利基,並提供了下面列出的優勢:

  • 可以整合C/C++的編擇流程,並整合成其編擇工具。
  • 在依賴管理上更貼近於現代程式語言的方式。
  • zig 語言的程式是可以與C/C++互通的,只需要用zig 編譯即可。

相較於把許多程式使用Rust改寫,Zig提供不同的切入角度來完善系統軟體的生態…

Reference:

檔案加密工具 – age

Hacker News上看到的新工具,age,一套可以用來加密檔案的工具;用法看起來也蠻簡單的,可以用其指令產生出public key,再用其來加密需要的檔案。

$ age-keygen -o key.txt 
Public key: age1ql3z7hjy54pw3hyww5ayyfg7zqgvc7w3j2elw8zmrj2kg5sfn9aqmcac8p 
$ tar cvz ~/data | age -r age1ql3z7hjy54pw3hyww5ayyfg7zqgvc7w3j2elw8zmrj2kg5sfn9aqmcac8p > data.tar.gz.age 
$ age --decrypt -i key.txt data.tar.gz.age > data.tar.gz

另外也支援多組key的加密,只有中其中一把解密的key 就可以解檔案了。

$ age -o example.jpg.age \
  -r age1ql3z7hjy54pw3hyww5ayyfg7zqgvc7w3j2elw8zmrj2kg5sfn9aqmcac8p \
  -r age1lggyhqrw2nlhcxprm67z43rta597azn8gknawjehu9d9dl0jq3yqqvfafg example.jpg

透過ssh-rsa key來做加密看也是可以的

$ age -R ~/.ssh/id_ed25519.pub example.jpg > example.jpg.age
$ age -d -i ~/.ssh/id_ed25519 example.jpg.age > example.jpg

看來蠻實用的,之後如果有相關的需求看來可以用來試試。

Reference:

Linux 的log 機制

會有這篇文章,主要還是因為在工作上踩了好幾次坑,很多次都遇到了log檔佔用了太多的硬碟空間,所以趁這機會好好研究一下linux上相關的設定。

在Linux上Distro,Log 相關的 daemon主要有以下幾個:
(1) rsyslogd
(2) systemd-journald
(3) logrotate.d
而它們各自有不同的工作要處理,下面就分別來介紹每個daemon service:

rsyslogd

 rsyslogd is the enhancement of syslogd, a service that takes care of managing centralized log files. Syslogd has been around for a long time.

https://www.golinuxcloud.com/systemd-journald-how-logging-works-rhel-7/

rsyslogd 算是syslogd的進化版本,相對於syslogd, 它提供更有效率的log 處理方式

預設的log 會寫入到/var/log/syslog 裡,而相關的設定檔則是放在/etc/rsyslog.conf/etc/rsyslog.d/50-default.conf裡面。

在ubuntu20.04中,rsyslog.conf 的設定來看,它預設是從imuxsock裡讀取到log 訊息,並寫入到/var/log/syslog之中。相關在rsyslog.conf的設定如下:

module(load="imuxsock") # provides support for local system logging

另外,在ubuntu中,是使用systemctl來執行這個daemon,所以我們可以透過下面這個指令來看其執行狀態:

sudo systemctl status rsyslog.service

而相關的輸出大概會是長這樣:

● rsyslog.service - System Logging Service
      Loaded: loaded (/lib/systemd/system/rsyslog.service; enabled; vendor preset: enabled)
      Active: active (running) since Thu 2021-08-26 11:12:02 UTC; 1 weeks 1 days ago
 TriggeredBy: ● syslog.socket
        Docs: man:rsyslogd(8)
              https://www.rsyslog.com/doc/
    Main PID: 269574 (rsyslogd)
       Tasks: 4 (limit: 9369)
      Memory: 517.1M
      CGroup: /system.slice/rsyslog.service
              └─269574 /usr/sbin/rsyslogd -n -iNONE

簡單講,rsyslogd 主要就是負責系統所有相關的log的寫入,像是/var/log/syslog, /var/log/auth.log, /var/log/kern.log
不過對於log 寫入之外的工作,就不是由它來負責的了,而是由另一個服務logrotate來負責的。

更改設定檔以後,則要使用sudo systemctl restart rsyslog.service來重啟服務以讀取新的設定檔

logrotate

從名稱上就可以看得出,這個service主要負責的工作就是做log file的rotate,所以當我們發現/var/log/syslog的檔案過大時,很大一部分的原因會是我們沒有設定好 /var/log/syslog的rotate設定。

logrotate 的設定檔位置在/etc/logrotate.conf,裡面有定義了預設的log檔案rotate設定;而針對於不同log的rotate 客制化設定,則是放在/etc/logrotate.d/ 這個資料夾下面。

舉例來說,是rsyslog的log rotate 設定的位置就是在/etc/logrotate.d/rsyslog 這裡。而相關的範例設定如下:

/var/log/syslog
 {
     rotate 7
     hourly
     maxsize 100M
     missingok
     notifempty
     delaycompress
     compress
     postrotate
         /usr/lib/rsyslog/rsyslog-rotate
     endscript
 }
 /var/log/mail.info
 /var/log/mail.warn
 /var/log/mail.err
 /var/log/mail.log
 /var/log/daemon.log
 /var/log/kern.log
 /var/log/auth.log
 /var/log/user.log
 /var/log/lpr.log
 /var/log/cron.log
 /var/log/debug
 /var/log/messages
 {
     rotate 4
     weekly
     missingok
     notifempty
     compress
     delaycompress
     sharedscripts
     postrotate
         /usr/lib/rsyslog/rsyslog-rotate
     endscript
 }

NOTE:
如果rotate的timesize都有設定的話,那就會以size為基準。而目前設定檔time最多只能支援到hourly
不過如果是以size為基準的話,應該也可以設定每分鐘的cron job,定期執行logrotate

另外,與一般的system service不一樣的地方是,logrotate是透過cron job的方式執行的,它的基本運作方式是,當我們執行了下面的指令去讀取新的rotate設定時

sudo logrotate /etc/logrotate.conf

logrotate 這個程式除了會去執行對應的rotate工作外,還會根據設定去產生對應的cron jobs。
舉例來說,如果我們設定syslog的rotate檢查頻率為hourly,那我們就可以找到對應的/etc/cron.hourly/logrotate 的cron job。

更改設定檔以後,則要使用sudo logrotate /etc/logrotate.conf來重啟服務以讀取新的設定檔

systemd-journald

 With the introduction of systemd, the journald log service systemd-journald has been introduced also. This service is tightly integrated with systemd, which allows administrators to read detailed information from the journal while monitoring service status using the systemctl status command.

https://www.golinuxcloud.com/systemd-journald-how-logging-works-rhel-7/

journald 是近年來在Linux上常被預設啟用的另一個log deamon service,它主要與systemd services 作整合,而與rsyslogd不同之處可以參考這裡:

Journald provides structure and indexed log files (called journals) in a secure manner. Therefore, not only are the journal files easier to search, it is harder for system intruders to cover their tracks.. Also you can maintain rsyslog messages in structured format.

Another advantage of using the systemd-journald service over traditional logging daemons is that journal files are automatically rotated if they grow above certain limits. This reduces log file maintenance issues and complexity.

The journalctl utility is used to read the journal binary log files and this command provides several means of filtering the data which is very powerful.

https://www.golinuxcloud.com/systemd-journald-how-logging-works-rhel-7/

簡單來說,它可以看成是rsyslogd的加強版,除了提供更安全的log儲存方式外,還額外提供了log rotate的功能,另外其搭配的cli journalctl還可以方便使用者檢索需要的log內容。

基本上journald的儲存相關設定主要是分為runtimesystem:

  • runtime: 所以runtime 相關的journals 會存放在/run/log/journal/下,一旦重新開機以後,舊的journals是不會保留的。
  • system: 如果下面的路徑/var/log/journal 存在且有對應的權限時,那麼journald就不會去寫log到/run/log/journal/底下了,而是持續性的寫入到/var/log/journal/下,在這情況下,即使重開機以後,舊的journals還是會保留的。

在ubuntu20.04中,journald的log 存放位置是在/var/log/journal,而設定檔的位置則是在/etc/systemd/journald.conf。 相關的設定檔說明可以參考ubuntu這文章

journald預設是透過systemd service的方式啟用的,所以透過下面的指令可以看得出目前journald的運作狀況:

sudo systemctl status systemd-journald.service

範例輸出結果為:

● systemd-journald.service - Journal Service
      Loaded: loaded (/lib/systemd/system/systemd-journald.service; static; vendor preset: enabled)
      Active: active (running) since Wed 2021-07-28 09:56:47 UTC; 1 months 7 days ago
 TriggeredBy: ● systemd-journald-dev-log.socket
              ● systemd-journald-audit.socket
              ● systemd-journald.socket
        Docs: man:systemd-journald.service(8)
              man:journald.conf(5)
    Main PID: 186 (systemd-journal)
      Status: "Processing requests…"
       Tasks: 1 (limit: 9369)
      Memory: 39.4M
      CGroup: /system.slice/systemd-journald.service
              └─186 /lib/systemd/systemd-journald
 Warning: journal has been rotated since unit was started, output may be incomplete.

journalctl 指令的使用

通常主要是透過journalctl指令來讀取log與變更log檔的一些設定,一些透過journalctl過濾/讀取log的相關技巧可以參考這邊

而一些常用journal 相關的管理指令如下:

  • 顯示目前所有journal的在硬碟上的使用空間
journalctl --disk-usage

  • 一次性的限制所有journal可以使用的硬碟空間 (e.g. 1GB),另外在清理時只會針對被archived 過的journal作清理。
sudo journalctl --vacuum-size=1G

更多journald 與rsyslog的整合與比較可以參考這裡

相關設定以確保log 檔案不會過大

在了解rsyslog, logroatejournald的一些運作模式以後,接下來回到之前常遇到的問題,log檔案size過大,而導致系統運作不正常的這個問題。

由於ubuntu20.04預設是同時會執行rsyslogdjournald的,所以基本上我們要關注的log檔案有兩個位置,分別是/var/log/syslog/var/log/journal

syslog的相關設定

基本上,針對sysloglogrotate預設是每天會做一次rotate,所以為了避免我們的應用程式在短時間製造過多的log,我們可以修改/etc/logrotate.d/rsyslog如下:

sudo vim /etc/logrotate.d/rsyslog

接著修改設定檔中的/var/log/syslog 部分如下

/var/log/syslog
 {
     rotate 3
     hourly
     maxsize 100M
     missingok
     notifempty
     delaycompress
     compress
     postrotate
         /usr/lib/rsyslog/rsyslog-rotate
     endscript
 }

以這樣來限制syslog的檔案大小

journald

以ubuntu20.04為例,它預設就是寫入到/var/log/journal,也就是說,我們必須去設定/etc/systemd/journald.conf 中的System*相關參數,詳細設定如下:

SystemMaxUse=1G
SystemKeepFree=
SystemMaxFileSize=100M
SystemMaxFiles=10

清除過大的log檔案

SYSLOG

如果目前的syslog 真的長的太大了,且裡面的內容是可丟棄的情況下,則可以使用下面的指令來清空目前的/var/log/syslog

#  this command needs to be ran as root
> /var/log/syslog

JOURNAL

如果是journal的話,則可以先將目前的journal archived,再做清理,詳細指令如下:

sudo journalctl --rotate
sudo journalctl --vacuum-time=1s

其它相關的清理方式可以參考這裡

reference:

如何有效的學習

在Hacker Daily News 上看到的一篇文章,在分享如何才會有效的學習。

作者分享了一些他的經歷以及相關的結論,但由於文章內容蠻長的,大概快速的掃過一遍以後,大概就節錄一些作者最後的列出的Key points。

Key Points

You can’t rely on intuition about how well your studying practices are working for you. Intuitive judgments of learning are often inaccurate and tend to produce an inflated perception of progress.

https://psyche.co/guides/how-research-from-psychology-can-help-you-study-effectively

不要用直覺來去評斷自己的學習成果,這通常會導致不正確的評估,最後可能高估自己的學習成果。

Avoid defaulting to habitual, passive approaches to studying such as rereading and highlighting sources. These do not take advantage of the reconstructive nature of memory, and make it more tedious and less effective.

https://psyche.co/guides/how-research-from-psychology-can-help-you-study-effectively

不要用一些習以為常的被動學習方式去學習,重複的閱讀重複的內容,或標註重點字句並不會讓學習變的有效。 (當你標註某個重點時,試著去理解為何它是重要的。)

Systematic engagement with the meaning of your source material is the key to successful studying.

https://psyche.co/guides/how-research-from-psychology-can-help-you-study-effectively

系統性的學習手頭上的資源會是非常重要的 !

Rather than cramming your studying into an extended session before the exam, it’s much more effective to distribute the time you have available for studying over a larger number of shorter sessions.

https://psyche.co/guides/how-research-from-psychology-can-help-you-study-effectively

在考試前的臨時抱彿腳對學習是沒有幫助的,試著有效的分配自己的時間,並且做好時間管理來學習才會真正有效學習。

When you are studying similar topics that might be easily confused, it’s a good idea to interleave your studying – to alternate between the topics during your study sessions. This can help you identify the differences between the topics and reduce the chances of them being conflated.

https://psyche.co/guides/how-research-from-psychology-can-help-you-study-effectively

對於類似的主題,如果有理解上有感到困惑的話,試著去交錯的去閱讀相關的主題,如此一來,會有助於理解不同主題的相似與相異之處。

You should view self-testing as an integral part of your studying. One way to do this is the read, recite and review (3R) method: read a section of text, set it aside as you try to recall its content in your own words, and then check your recall, repeating as necessary.

https://psyche.co/guides/how-research-from-psychology-can-help-you-study-effectively

自我評量也是非常重要的,有效的學習通常伴隨著有效的衡量方式;作著提出了3R (Read, Recite, Review) 來幫助自己評斷學習成果。

想法

通常對於這種類似文章,我都會稍微掃過就好;不過這篇文章整理出了一些重點,而且這些節錄的重點又都是以前曾勿略的地方,剛好趁這個機會作點記錄讓自己以後可以有些回顧。

Reference:

How to study effectively | Psyche Guides

Incremental ETL

Databrick blog 上看到的一篇文章,剛好在提到一些資料處理概念,剛好也可以複習一下相關的知識。

et’s first dive into what exactly incremental ETL is. At a high level, it is the movement of data between source and destination – but only when moving new or changed data. The data moved through incremental ETL can be virtually anything – web traffic events or IoT sensor readings (in the case of append data) or changes in enterprise databases (in the case of CDC).

https://databricks.com/blog

https://databricks.com/blog

概念上,就是將原始收集到的資料,透過每個階段的ETL job,來整理與清洗資料,並且再存入下一個資料儲存的地方;而每個處理的階段就因組織而異了,但最終目的就是希望真的需要用某些資料時,它已經是處理好在那了。
另外,大量的原始/歷史資料其實就可以存在像Datalake 這類型的地方,以減少儲存成本。

對於Data driven的組織來說,這樣的架構設計可以讓資料的處理更有效率,且大量的原始資料若存放在Datalake 則又可以降低營運成本。

但相對的,也有些負面的取捨如下:

  • 每個資料處理階段,可能都會需要額外的ETL pipeline來處理,而這些都會是營運上的成本。
  • 當一筆原始資料進入時,對於最下游的服務來說,需要隔一段(ETL處理)時間才會反應再下游服務的資料中,所以對於資料有立即性的需求可能就會不太適合。
  • 若要更新原始資料,並且更新所有下游的資料會是非常困難的事情。

新的Docker Desktop 費用

對有在使用Docker 當作日常開發工具的開發者來說,這應該是個值得注意的消息,也就是Docker Desktop 現在已經不是完全免費了!

對稍大型的企業(人數>=250人或年度收入>=$10M USD)來說,它們可能必須購買相關的授權才能夠繼續使用;然後新聞內容有提到,若是使用Docker command line的話,還是維持免費。

Docker Desktop主要是包了很多功能,像是image的管理,container的管理…等等。

UPDATED: DK 的相關文章有提到如何用macport 安裝docker engine,到時真的有需要時可以來參考一下XD

Reference

Docker Desktop no longer free for large companies: New ‘Business’ subscription is here • The Register