简单博客

Go Empty Struct

September 1, 2023
Go
Mark

package main

import (
	"fmt"
	"unsafe"
)

func main() {
	type A struct{}
	type B struct{}
    // 结构体里的字段都是`Empty Struct`时,占用空间为0
	type S struct {
		A A
		B B
	}
	var s S
	fmt.Println(unsafe.Sizeof(s)) // prints 0
    // 如果是指针,占用空间为8
	fmt.Println(unsafe.Sizeof(&s)) // prints 8

	var x [1000000000]struct{}
    // 可以同时存储A和B类型元素
	x[0] = A{}
	x[1] = B{}
	fmt.Println(unsafe.Sizeof(x)) // prints 0
    // 地址一样
	fmt.Printf("%p, %p", &x[0], &x[1]) // 0x54e3a0, 0x54e3a0
}

See also

Jupyter notebook

August 3, 2023

Install: pip install --user jupyter.

Install plugin: jupyter install matplotlib.

Install rust tools: cargo install evcxr_jupyter && evcxr_jupyter --install.

Install plugin: jupyter install plotters.

Start: jupyter notebook --port 35222.

Vscode Translate

July 25, 2023
Vscode
Translate

试了几个vscode的翻译插件,包括Google Translate, Comment Translate, Translate Var

来自intellsmiComment Translate最终脱颖而出。

它支持Google, Bing, Baidu等翻译工具。

设置:选择翻译工具Bing,再设置源语言和目标语言。

使用:选中单词即会自动翻译,非常方便。

Windows restart remote service

July 25, 2023
Windows
Restart-Remote-Service

Windows如何方便的重启远程服务器里的服务,在不使用远程连接的情况下?

注意:服务(名称:service-name)已在远程机器上创建。

  1. 在本机新建映射网络驱动器.

  2. 打开映射好的文件夹,在其中添加batps1文件:

restart.bat:

@echo off
for %%i in (service-name) do (
    echo the service '%%i' is being starting...
    sc query %%i
    net stop %%i
    net start %%i
    sc query %%i
    echo service '%%i' started.
)
pause

restart.ps1:

$Username = 'Name'
$Password = 'Password'
$pass = ConvertTo-SecureString -AsPlainText $Password -Force
$Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$pass
Invoke-Command -ComputerName [Remote-IP] -ScriptBlock { Get-Service WinRM } -credential $Cred
Invoke-Command -ComputerName [Remote-IP] -credential $Cred -ScriptBlock { E:\win-dms4\restart.bat } # 此处指定上述bat文件在远程机器上的决对路径
Read-Host -Prompt "Press Enter to exit"

完成后,只要在本机打开映射的文件夹,使用PowerShell执行restart.ps1脚本,即可快速重启指定服务。

错误的定义和返回

July 21, 2023

错误的定义和返回

错误的定义 #

错误粒度:太细则既多又杂,太宽则毫无意义。

个人觉得一般需要的错误有以下:正常、参数错误、业务错误、内部错误、返回错误。业务错误又有:无权限、处理超时、无记录、已经存在。

可参照 GRPC的实现

CodeNumberDescription
OK0Not an error; returned on success.
CANCELLED1The operation was cancelled, typically by the caller.
UNKNOWN2Unknown error. For example, this error may be returned when a Status value received from another address space belongs to an error space that is not known in this address space. Also errors raised by APIs that do not return enough error information may be converted to this error.
INVALID_ARGUMENT3The client specified an invalid argument. Note that this differs from FAILED_PRECONDITION. INVALID_ARGUMENT indicates arguments that are problematic regardless of the state of the system (e.g., a malformed file name).
DEADLINE_EXCEEDED4The deadline expired before the operation could complete. For operations that change the state of the system, this error may be returned even if the operation has completed successfully. For example, a successful response from a server could have been delayed long
NOT_FOUND5Some requested entity (e.g., file or directory) was not found. Note to server developers: if a request is denied for an entire class of users, such as gradual feature rollout or undocumented allowlist, NOT_FOUND may be used. If a request is denied for some users within a class of users, such as user-based access control, PERMISSION_DENIED must be used.
ALREADY_EXISTS6The entity that a client attempted to create (e.g., file or directory) already exists.
PERMISSION_DENIED7The caller does not have permission to execute the specified operation. PERMISSION_DENIED must not be used for rejections caused by exhausting some resource (use RESOURCE_EXHAUSTED instead for those errors). PERMISSION_DENIED must not be used if the caller can not be identified (use UNAUTHENTICATED instead for those errors). This error code does not imply the request is valid or the requested entity exists or satisfies other pre-conditions.
RESOURCE_EXHAUSTED8Some resource has been exhausted, perhaps a per-user quota, or perhaps the entire file system is out of space.
FAILED_PRECONDITION9The operation was rejected because the system is not in a state required for the operation’s execution. For example, the directory to be deleted is non-empty, an rmdir operation is applied to a non-directory, etc. Service implementors can use the following guidelines to decide between FAILED_PRECONDITION, ABORTED, and UNAVAILABLE: (a) Use UNAVAILABLE if the client can retry just the failing call. (b) Use ABORTED if the client should retry at a higher level (e.g., when a client-specified test-and-set fails, indicating the client should restart a read-modify-write sequence). (c) Use FAILED_PRECONDITION if the client should not retry until the system state has been explicitly fixed. E.g., if an “rmdir” fails because the directory is non-empty, FAILED_PRECONDITION should be returned since the client should not retry unless the files are deleted from the directory.
ABORTED10The operation was aborted, typically due to a concurrency issue such as a sequencer check failure or transaction abort. See the guidelines above for deciding between FAILED_PRECONDITION, ABORTED, and UNAVAILABLE.
OUT_OF_RANGE11The operation was attempted past the valid range. E.g., seeking or reading past end-of-file. Unlike INVALID_ARGUMENT, this error indicates a problem that may be fixed if the system state changes. For example, a 32-bit file system will generate INVALID_ARGUMENT if asked to read at an offset that is not in the range [0,2^32-1], but it will generate OUT_OF_RANGE if asked to read from an offset past the current file size. There is a fair bit of overlap between FAILED_PRECONDITION and OUT_OF_RANGE. We recommend using OUT_OF_RANGE (the more specific error) when it applies so that callers who are iterating through a space can easily look for an OUT_OF_RANGE error to detect when they are done.
UNIMPLEMENTED12The operation is not implemented or is not supported/enabled in this service.
INTERNAL13Internal errors. This means that some invariants expected by the underlying system have been broken. This error code is reserved for serious errors.
UNAVAILABLE14The service is currently unavailable. This is most likely a transient condition, which can be corrected by retrying with a backoff. Note that it is not always safe to retry non-idempotent operations.
DATA_LOSS15Unrecoverable data loss or corruption.
UNAUTHENTICATED16The request does not have valid authentication credentials for the operation.

实现时,错误码范围控制在0255,将035留作预定义错误码,36~255留作自定义错误码。

...

Prometheus start failed

July 6, 2023
Prometheus
Prometheus

Question #

prometheus start failed:

...
ts=2023-07-06T01:34:43.871Z caller=repair.go:57 level=info component=tsdb msg="Found healthy block" mint=1688493607159 maxt=1688515200000 ulid=01H4J6TB6NCBZFNR9XZ1R2P67H
ts=2023-07-06T01:34:43.871Z caller=repair.go:57 level=info component=tsdb msg="Found healthy block" mint=1688529607159 maxt=1688536800000 ulid=01H4JDM4YR6J3TJVBY6P6EGZS4
ts=2023-07-06T01:34:43.872Z caller=repair.go:57 level=info component=tsdb msg="Found healthy block" mint=1688536807159 maxt=1688544000000 ulid=01H4JMFGKWY1MAM7GBFDQ89FRV
ts=2023-07-06T01:34:43.872Z caller=main.go:696 level=info msg="Stopping scrape discovery manager..."
ts=2023-07-06T01:34:43.872Z caller=main.go:710 level=info msg="Stopping notify discovery manager..."
ts=2023-07-06T01:34:43.872Z caller=main.go:732 level=info msg="Stopping scrape manager..."
ts=2023-07-06T01:34:43.872Z caller=manager.go:946 level=info component="rule manager" msg="Stopping rule manager..."
ts=2023-07-06T01:34:43.872Z caller=manager.go:956 level=info component="rule manager" msg="Rule manager stopped"
ts=2023-07-06T01:34:43.872Z caller=notifier.go:601 level=info component=notifier msg="Stopping notification manager..."
ts=2023-07-06T01:34:43.872Z caller=main.go:726 level=info msg="Scrape manager stopped"
ts=2023-07-06T01:34:43.872Z caller=main.go:692 level=info msg="Scrape discovery manager stopped"
ts=2023-07-06T01:34:43.872Z caller=main.go:907 level=info msg="Notifier manager stopped"
ts=2023-07-06T01:34:43.872Z caller=main.go:706 level=info msg="Notify discovery manager stopped"
ts=2023-07-06T01:34:43.872Z caller=tls_config.go:195 level=info component=web msg="TLS is disabled." http2=false
ts=2023-07-06T01:34:43.872Z caller=main.go:916 level=error err="opening storage failed: get segment range: segments are not sequential"

Answer #

rm -rf /var/lib/prometheus/metrics2/

Path is from prometheus -h | grep '--storage.tsdb.path'.

...

Compare and Order

July 5, 2023
Compare, Order
Thought

比,争。

比较,争先。

拿他与她比,让他与她争。

做黄雀,做渔翁。

不比,不争。

不付出无收获?

丛林社会,谈何付出,谈何收获。

有付出也无收获。

ml sklearn

June 26, 2023
Sklearn
Ml

尝试 #

$ pip install scikit-learn
$ python3.10
>>> from sklearn.datasets import load_iris
>>> from sklearn.linear_model import LogisticRegression
>>> data, y = load_iris(return_X_y=True)
>>> clf = LogisticRegression(random_state=0, max_iter=1000).fit(data, y)
>>> clf.predict(data[:2, :])
>>> clf.predict_proba(data[:2, :])
>>> clf.score(data, y)

查找操作记录 #

cat ~/.python_history.

模型、策略、优化算法 #

模型是输入输出函数:Y = F(X).

策略是拟合过程的损失函数:L(Y, F(X)), 可以是均方误差、对数损失函数、交叉熵损失函数。

优化算法:确定模型和损失函数后,可以加速计算的方法,比如:随机梯度下降法、牛顿法、拟牛顿法。

windows route

June 13, 2023
Route
Route

Windows通过route命令配置路由 #

route add 192.168.0.0 mask 255.255.0.0 192.168.66.254 -p

192.168.0.0: 目标主机的网络地址

mask 255.255.0.0: 掩码,与目标网络地址对应

192.168.66.254: 网关地址

Linux通过ip route命令配置路由 #

NOTE: ip routeroute命令的升级版本,但route命令仍在大量使用。

# 设置192.168.4.0网段的网关为192.168.166.1,数据走wlan0接口
# /24 is the network prefix. The network prefix is the number of enabled bits in the subnet mask.
# 24位子网掩码
ip route add 192.168.4.0/24 via 192.168.166.1 dev wlan0

# 255.255.255.0为子网掩码
# 3*8(255即是8位二进制)
ip route add 192.168.0.0/255.255.255.0 dev eth0

子网掩码 #

ip地址包含了网络地址和主机地址两部分,怎么区分呢?

...

wsl2初始化Mysql数据库速度非常慢

June 1, 2023
Mysql, Wsl2
Mysql

wsl2版本 #

> wsl.exe -v
WSL version: 1.2.5.0
Kernel version: 5.15.90.1
WSLg version: 1.0.51
MSRDC version: 1.2.3770
Direct3D version: 1.608.2-61064218
DXCore version: 10.0.25131.1002-220531-1700.rs-onecore-base2-hyp
Windows version: 10.0.19045.3031

使用过程中,因为磁盘空间问题,把子系统安装位置从C盘转移到了其它盘。

操作 #

sql目录里的*.sql文件逐一导入到8.0.33版本的Mysql

尽管sql文件不多也不大,但是整个过程非常慢。其中一个有一千个左右的INSERT IGNORE语句,更是用了将近12分钟才完成。

怎么办? #

改为通过网络访问本机的数据库。

端口 #

一文中提到:

根据我的观察, 如果Windows本地启动了指定端口, 这时WSL2中虽然可以使用相同的端口, 但是localhost:port 将指向Windows的服务, WSL的服务将会被覆盖!

当然了, 如果我们配置了端口转发, 转发的IP是WSL的地址, 而不是localhost, 那么WSL将会覆盖Windows的服务!

而我的观察是,

  1. 我发现本地起了数据库服务之后,在wsl2里起数据库服务(mysql服务,端口都是3306)的情况下,是不会报端口重复绑定错误的。

  2. 但是如果我在wsl2里先起一个服务,绑定端口14222后,再在主机起相同服务,想绑定相同端口时,则会报错端口已被绑定。

  3. 如果我是先在主机起上述服务,然后再在wsl2起该服务,则能正常启动。那在主机访问localhost:[port]时会访问到哪个呢?此时访问到的是主机的服务。

所以端口是否占用会不会还跟服务起的顺序有关呢?

暂时未看到有确切的描述。

但是,经过上面的实验,可以认为:

先在主机起服务,再在wsl2起服务绑定相同端口时,服务可正常启动;在主机访问`localhost:[port]`时访问的是主机的服务;在wsl2里访问的则是wsl2的服务,除非手动指定主机IP。

而如果先在wsl2里起了服务,再在主机起服务(绑定相同端口: 14222),则会报错端口已被绑定。

但是,如果在wsl2里先起的mysql服务,再在主机起,则不会报错,所以还跟端口值有关?

最新发现 #

如何通过局域网访问WSL2中的服务?

假设局域网上有两台主机A和B。主机A安装了WSL2、开启了Redis服务,端口为6379。现在主机B如何才能访问主机A上WSL2的Redis服务呢?

  1. 配置端口转发

1). 以管理员权限打开PS,输入命令:

...