Go语言的函数返回多个值

以下摘自The Go Programming Language

Often, functions use these additional results to indicate some kind of error, either by returning an error as in the call to os.Open, or a bool, usually called ok. If a map lookup, type assertion, or channel receive appears in an assignment in which two results are expected, each produces an additional boolean result:
v, ok = m[key] // map lookup
v, ok = x.(T) // type assertion
v, ok = <-ch // channel receive
As with variable declarations, we can assign unwanted values to the blank identifier:
_, err = io.Copy(dst, src) // discard byte count
_, ok = x.(T)

map查找,类型断言和从channel接收数据都会返回两个值,其中一个是表示成功或失败的布尔值。同变量定义一样,不想要的值可以赋给_

 

Linux kernel 笔记 (60)——scheduling domain

NUMA系统上,由于不同CPU直接访问本地内存和远端内存的时间相差很大,所以更好地调度算法就显得很重要。Linux kernel引入了scheduling domain的概念。可以参看下面例子:

[root@localhost ~]# cd /proc/sys/kernel/sched_domain/
[root@localhost sched_domain]# ls
cpu0  cpu1  cpu2  cpu3  cpu4  cpu5  cpu6  cpu7
[root@localhost sched_domain]# ls -alt *
cpu0:
total 0
dr-xr-xr-x. 1 root root 0 Feb 26 19:37 domain0
dr-xr-xr-x. 1 root root 0 Feb 26 19:37 domain1
dr-xr-xr-x. 1 root root 0 Feb 26 19:37 .
dr-xr-xr-x. 1 root root 0 Feb 26 19:37 ..

cpu1:
total 0
dr-xr-xr-x. 1 root root 0 Feb 26 20:06 domain0
dr-xr-xr-x. 1 root root 0 Feb 26 20:06 domain1
dr-xr-xr-x. 1 root root 0 Feb 26 19:37 .
dr-xr-xr-x. 1 root root 0 Feb 26 19:37 ..

cpu2:
total 0
dr-xr-xr-x. 1 root root 0 Feb 26 20:06 domain0
dr-xr-xr-x. 1 root root 0 Feb 26 20:06 domain1
dr-xr-xr-x. 1 root root 0 Feb 26 19:37 .
dr-xr-xr-x. 1 root root 0 Feb 26 19:37 ..

cpu3:
total 0
dr-xr-xr-x. 1 root root 0 Feb 26 20:06 domain0
dr-xr-xr-x. 1 root root 0 Feb 26 20:06 domain1
dr-xr-xr-x. 1 root root 0 Feb 26 19:37 .
dr-xr-xr-x. 1 root root 0 Feb 26 19:37 ..

cpu4:
total 0
dr-xr-xr-x. 1 root root 0 Feb 26 20:06 domain0
dr-xr-xr-x. 1 root root 0 Feb 26 20:06 domain1
dr-xr-xr-x. 1 root root 0 Feb 26 19:37 .
dr-xr-xr-x. 1 root root 0 Feb 26 19:37 ..

cpu5:
total 0
dr-xr-xr-x. 1 root root 0 Feb 26 20:06 domain0
dr-xr-xr-x. 1 root root 0 Feb 26 20:06 domain1
dr-xr-xr-x. 1 root root 0 Feb 26 19:37 .
dr-xr-xr-x. 1 root root 0 Feb 26 19:37 ..

cpu6:
total 0
dr-xr-xr-x. 1 root root 0 Feb 26 20:06 domain0
dr-xr-xr-x. 1 root root 0 Feb 26 20:06 domain1
dr-xr-xr-x. 1 root root 0 Feb 26 19:37 .
dr-xr-xr-x. 1 root root 0 Feb 26 19:37 ..

cpu7:
total 0
dr-xr-xr-x. 1 root root 0 Feb 26 20:06 domain0
dr-xr-xr-x. 1 root root 0 Feb 26 20:06 domain1
dr-xr-xr-x. 1 root root 0 Feb 26 19:37 .
dr-xr-xr-x. 1 root root 0 Feb 26 19:37 ..

/proc/sys/kernel/sched_domain/目录下每个CPU都有一个自己的目录,并且每个CPU目录下都有和自己相关的domain信息。

multi-level系统中,也拥有multi-levelscheduling domain(内核中结构体是struct sched_domain)。每个scheduling domain包含一组共享属性和调度策略的CPU;每个scheduling domain包含至少一个或多个CPU group(内核中结构体是struct sched_group),每个CPU group会被scheduling domain看做一个独立的单元。

scheduling domain的核心代码位于kernel\sched\core.c中,关于/proc/sys/kernel/sched_domain/cpu$/domain$中各个文件的含义,都可以在这里找到。

NUMA系统上,如果一个node利用率非常高,比如高于90%,而另一个node利用率可能只有60%~70%,这时可以尝试disable wakeup affinity

参考资料:
Scheduling domains
sched-domains.txt
Does domain0 in /proc/sys/kernel/sched_domain/cpu$ refer top-level domain in the system?
How to understan /proc/sys/kernel/sched_domain/cpu$/domain$/flags?

docker笔记(8)—— Container的生命周期

docker create创建一个containerdocker start则启动这个container

docker run相当于docker create加上docker start。需要注意的是,一旦执行完container需要做的工作,container就会退出:

# docker run hello-world

Hello from Docker.
......

# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
62a13663511f        hello-world         "/hello"            5 seconds ago       Exited (0) 4 seconds ago                       compassionate_ptolemy

停止container可以使用下列两个命令:
docker kill:向container发送SIGKILL信号;
docker stop:先向container发送SIGTERM信号,过一段时间再发送SIGKILL信号。

彻底干掉container使用docker rm命令。移除所有已经停止的container可以使用下列命令:

$ docker rm $(docker ps -a -q)

参考资料:
Docker Cookbook

 

docker笔记(7)—— Index,registry和repository

看一下对三个词的英文解释:

An index manages user accounts, permissions, search, tagging, and all that nice stuff that’s in the public web interface.

A registry stores and serves up the actual image assets, and it delegates authentication to the index. Docker registry is a service that is storing your docker images.

Docker repository is a collection of different docker images with same name, that have different tags.

Index是管理docker的服务(包含用户账户,操作权限,等等)。Registry是存储docker image的服务。Repository则是包含同名不同tagdocker image的集合。

参考资料:
Difference between Docker registry and repository
Where are Docker images stored?

 

docker笔记(6)—— 在docker container中执行命令的脚本

下面脚本的功能是循环地在各个container中执行命令:

#!/bin/bash -x

for i in {1..2}
do
        docker exec -i hammerdb_net${i} bash <<-EOF
        su oracle
        source /tmp/ora_env
        cd /data/oracle/tablespaces/
        rm -f *.html
        ./create_awr.sh
        mv awr.html awr_${i}.html
        EOF
done

需要注意的是在dodone之间应该使用tab而不是空格。

参考资料:
Indenting bourne shell here documents
How to write a bash script which automate entering “docker container” and doing other things?
Can’t indent heredoc to match nesting’s indent

 

Haskell笔记 (19)—— Guard

guard跟在函数名和参数之后,用管道符号表示:

myCompare :: (Ord a) => a -> a -> Ordering  

myCompare a b
    | a < b = LT
    | a == b = EQ
    | otherwise = GT    

guard长得和pattern类似,pattern检查输入是否符合这个pattern,而guard则会计算布尔表达式,返回TrueFalse。如果返回值是True,则执行等号后面的函数体部分。

看另外一个例子:

Capture

The first pattern specifies that if we try to take a 0 or negative number of elements, we get an empty list. Notice that we’re using _ to match the list because we don’t really care what it is in this case. Also notice that we use a guard, but without an otherwise part. That means that if n turns out to be more than 0, the matching will fall through to the next pattern.

guard没有otherwise,如果都不匹配的话,就会执行下一个pattern

参考资料:
Guards, guards!

Oracle笔记(5)——连接数据库

Oracle的网络协议称之为Oracle NetSQL*NetNet8

连接数据库的两种方式:

(1)本地连接(Local

这种方式client和数据库位于同一台服务器上,数据通信不需要经过listener进程,而是通过Unix pipe方式进行通信。比如以SYS用户连接数据库:

sqlplus as / sysdba

或是利用ORACLE_SID环境变量指定本地数据库,然后使用用户名和密码进行连接:

sqlplus username/password

需要注意的是还有一个TWO_TASK环境变量指定远端数据库,而当这两个变量同时指定时,TWO_TASK优先级更高。

(2)远端连接(remote

这种方式client和数据库位于不同服务器上,连接时需要指定连接的数据库TNS名字:

sqlplus username/password@orcl

这种方式需要利用tnsnames.ora文件来解析数据库主机和端口,此外数据通信也要经过listener进程。

区分“本地连接”和“远端连接”的一个方法就是看连接数据库时是否指定数据库TNS名字:@tns_alia

(3)

Oracle client和数据库之间通信使用TNS(Transparent Network Substrate)协议,可以用wireshark解析:

Capture

 

参考资料:

research oracle tns protocol

Oracle Database Communication Protocol

Oracle 12c For Dummies ;

http://ora-exp.blogspot.com/2007/06/oraclesid-and-twotask-environment_21.html

Re: sqlplus connection from unix with and without the oracle sid

In Oracle, how do you change the ‘default’ database?

Should we consider “sqlplus” as an Oracle client?

SYSDBA password in Oracle DB

 

Oracle笔记(4)——tnsnames.ora文件

tnsnames.ora文件包含着Oracle数据库连接信息,是供client使用的(TNS代表Transport Network Substrate),位于$ORACLE_HOME/network/admin目录。举例如下:

ORCL =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = npar1.xxxxxx.com)(PORT = 9005))
     (CONNECT_DATA =
      (SID = ORCL)
      (SERVER = DEDICATED)
      (SERVICE_NAME = ORCL)
    )
  )

client发起连接数据库请求:“"connect system/system@ORCL"”,就需要解析tnsnames.ora来获得Oracle数据库的地址和端口信息。在配置文件中,HOST指定数据库instance运行的主机,PORTlistener进程监听的端口。

参考资料:
LISTENER.ORA and TNSNAMES.ORA
Local Naming Parameters in the tnsnames.ora File

 

docker笔记(5)—— Volume

Docker image是一系列的read-only layer。当启动container以后,在read-only layer之上会增加一个read-write layer。当需要对文件进行修改时,就要把数据从read-only layer拷贝到read-write layer。因此,image的数据不会被破坏,container相当于操作的是image数据的副本。这种read-only layerread-write layer的结合称之为Union File System

因此,为使对数据的修改能够保存下来,docker引入了volume概念:volume即是位于主机文件系统上,而不是Union File System上的目录和文件。

(1)

$ docker run -it --name container-test -h CONTAINER -v /data debian /bin/bash
root@CONTAINER:/# ls /data
root@CONTAINER:/#   

container-test中的/data目录位于主机的位置:

$ docker inspect -f {{.Volumes}} container-test
map[/data:/var/lib/docker/vfs/dir/cde167197ccc3e138a14f1a4f7c0d65b32cecbada822b0db4cc92e79059437a9] 

(2)

$ docker run -v /home/adrian/data:/data debian ls /data

这种方式直接把主机上的目录映射到container中的一个目录。

参考资料:
Understanding Volumes in Docker

Oracle笔记(3)——Net Listener进程

Oracle Net Listener是一个独立的进程,负责处理client的请求,并把这些请求分发给相应的database instance。配置文件是listener.ora,位于$ORACLE_HOME/network/admin目录下。修改这个文件必须重启listener进程。

listener.ora如下所示:

# listener.ora Network Configuration File: /u01/app/oracle/product/12.1.0/dbhome_1/network/admin/lis
tener.ora
# Generated by Oracle configuration tools.
SID_LIST_LISTENER =
 (SID_LIST =
  (SID_DESC =
   (GLOBAL_DBNAME = db01)
   (ORACLE_HOME = /u01/app/oracle/product/11.1.0/db_1)
   (SID_NAME = db01)
  )
  (SID_DESC =
   (GLOBAL_DBNAME = dev12c)
   (ORACLE_HOME = /u01/app/oracle/product/12.1.0/dbhome_1)
   (SID_NAME = dev12c)
  )
 )
LISTENER =
 (DESCRIPTION_LIST =
  (DESCRIPTION =
   (ADDRESS = (PROTOCOL = TCP)(HOST = orasvr01)(PORT = 1521))
  )
  (DESCRIPTION =
   (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
  )
 )

SID_LIST_LISTENER部分指定listener进程需要处理的数据库连接;而LISTENER部分指定这些数据库所在的主机,以及listener进程需要监听的端口。

在数据库能够服务客户端请求之前,必须要启动listener进程。使用lsnrctl命令可以启动和停止listener进程:

$ lsnrctl start
$ lsnrctl stop

lsnrctl status命令可以用来检查listener的状态。

$ lsnrctl status

LSNRCTL for Linux: Version 12.1.0.2.0 - Production on 16-FEB-2016 04:55:25

Copyright (c) 1991, 2014, Oracle.  All rights reserved.

Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=npar1.atc-hp.com)(PORT=9012)))
STATUS of the LISTENER
------------------------
Alias                     LISTENER
Version                   TNSLSNR for Linux: Version 12.1.0.2.0 - Production
Start Date                04-FEB-2016 01:01:06
Uptime                    12 days 3 hr. 54 min. 18 sec
Trace Level               off
Security                  ON: Local OS Authentication
SNMP                      OFF
Listener Parameter File   /app/oracle/product/12.1.0/dbhome_1/network/admin/listener.ora
Listener Log File         /app/oracle/diag/tnslsnr/npar1/listener/alert/log.xml
Listening Endpoints Summary...
  (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=npar1.atc-hp.com)(PORT=9012)))
Services Summary...
Service "ORCL2" has 1 instance(s).
  Instance "ORCL2", status UNKNOWN, has 1 handler(s) for this service...
The command completed successfully

参考资料:
Configuring and Administering Oracle Net Listener
How to Configure the Database Listener with Listener.ora in Oracle 12c