[컴][db] mongodb 성능 분석, MongoDB Profiler and explain()

 몽고db분석 / 빠르게 / 성능 분석/ profiler /

mongodb 성능 분석, MongoDB Profiler and explain()

db.getProfilingStatus()

db.getProfilingLevel()
db.setProfilingLevel(1)
  • level 0 : 아무런 data 도 수집하지 않는다. 이것이 default 값이다.
  • level 1 : slowms 값보다 오래 걸리는 query 에 대한 data 를 수집한다.
  • level 2 : 모든 operation 에 대한 data 를 수집한다.
db.system.profile.find({
"command.pipeline": { $exists: true }
}, {
"command.pipeline":1
}).sort({$natural:-1}).pretty();

COLLSCAN 을 하는 모든 query 들

// all queries that do a COLLSCAN
db.system.profile.find({"planSummary":{$eq:"COLLSCAN"},
"op" : {$eq:"query"}}).sort({millis:-1})

range scan 또는 full scan 을 하는 query 들

db.system.profile.find({"nreturned":{$gt:1}})

가장느린 query 중 상위 10 개

// 가장느린 query 중 상위 10 개
db.system.profile.find({"op" : {$eq:"query"}}, {
"query" : NumberInt(1),
"millis": NumberInt(1)
}
).sort({millis:-1},{$limit:10}).pretty()


// 가장느린 10개의 aggregation 들
// the source of the top ten slowest commands/aggregations
db.system.profile.find({"op" : {$eq:"command"}}, {
"command.pipeline" : NumberInt(1),
"millis": NumberInt(1)
}
).sort({millis:-1},{$limit:10}).pretty()

10ms 이상 걸리는 query

//find all queries that take more than ten milliseconds, in descending order, 
displaying both queries and aggregations
db.system.profile.find({"millis":{$gt:10}}) .sort({millis:-1})

See Also

  1. MongoDB Aggregation Pipeline | MongoDB
  2. Aggregation Pipeline Optimization — MongoDB Manual

[컴] windows batch 가 중간에 끝나버리는 현상 해결방법

 

batch programming / 중간에 끝 / end before all the commands are executed / aborting the execution of the calling batch file

windows batch 가 중간에 끝나버리는 현상 해결방법

npm run build 같은 명령어를 .bat 안에 넣어서 실행하면, npm run build 가 끝나고, batch 가 그냥 끝나버린다.

이것에 대한 해결책은 2가지가 있다.

call

아래처럼 call 을 이용하면 된다. call 에 대한 자세한 내용은 ref. 3 을 참고하자.

echo ============
echo build
echo ============
call npm run build
if /I "%ERRORLEVEL%" NEQ "0" (
    echo build error
    pause
    goto exit
)


echo ============
echo zip dist folder
echo ============
set zip="c:\Program Files\7-Zip\7z.exe"
set yyyymmdd=%date:~0,4%%date:~5,2%%date:~8,2%
set hhmmss=%time:~0,2%%time:~3,2%%time:~6,2%
set filename=html-%yyyymmdd%-%hhmmss%.zip

%zip% a -tzip -mx1 %filename% .\dist\*
if /I "%ERRORLEVEL%" NEQ "0" (
    echo zip error
    pause
    goto exit
)

&& 사용하기

&& 은 앞의 명령어가 정상적으로 끝나면 그 다음 명령어를 실행해 준다. 자세한 내용은 ref. 2 참고하자.

그래서 아래처럼 && 을 사용해주면, npm run build 가 끝난 후 계속해서 command 를 실행하게 된다.

set zip="c:\Program Files\7-Zip\7z.exe"
set yyyymmdd=%date:~0,4%%date:~5,2%%date:~8,2%
set hhmmss=%time:~0,2%%time:~3,2%%time:~6,2%
set filename=html-%yyyymmdd%-%hhmmss%.zip

npm run build ^
&& %zip% a -tzip -mx1 %filename% .\dist\*

References

  1. Batch file stops running after the first command - Stack Overflow
  2. cmd - What does “&&” do in this batch file? - Stack Overflow
  3. Batch files - CALL

[컴] read file in nodejs

 

read file in nodejs / nodejs file read

read file in nodejs

util.promisify 를 이용해서 async/await 을 사용했다. 특정 offset 부터 읽고 싶을 때 사용할 수 있다. 주의할 점은 아래 코드는 마지막 부분에 file size 를 넘어갈 수 있는데, 그부분에 대한 고려는 없다. 관련해서는 ref. 1을 참고하면 될 것 같다.

import * as fs from 'fs';
import * as util from 'util';
...

const open = util.promisify(fs.open);
const fstat = util.promisify(fs.fstat);
const fwriteFile = util.promisify(fs.writeFile);
...   
const offsetStart = 1000
const offsetEnd = 2000
const fd = await open("11-00", "r");
const fstats = await fstat(fd)
const fileSize = fstats.size;
const bufferOffset = 0
const chunkSize = offsetEnd - offsetStart + 1
const buffer = Buffer.alloc(chunkSize);

const readSize = fs.readSync(fd, buffer, bufferOffset, chunkSize, offsetStart);

console.log(buffer)

await fwriteFile('res.txt', JSON.stringify(buffer) + '\n', { flag: 'a+' });

See Also

  1. 쿠…sal: [컴] nodejs 의 readline 사용법

Reference

  1. node.js - How to read a file by setting correct offset and position and write to the response in Nodejs with manual buffering? - Stack Overflow
  2. File system | Node.js v17.3.0 Documentation

[컴] wsl2 에서 mongodb 접근

wsl /windows subsystem for linux  /mongodb client / mongo tool / mongodb tools / wsl 에서 mongodb 접근

wsl2 에서 mongodb 접근

기본적으로 see also. 1 과 같은 과정을 거친다.

  1. db port 를 열고
  2. db 가 외부에서 접근 하도록 db 설정 변경

외부접근이 가능하도록 mongodb 설정

방화벽에서 port 열기

windows 에서는 아래 command 를 이용해서 27017 에 대한 port 를 열어준다.

netsh advfirewall firewall add rule name= "mongodb port for wsl2" dir=in action=allow protocol=TCP localport=27017 remoteip=172.0.0.1-172.254.254.254

mongodb 의 bind ip 변경

windows 에서는 아래 경로에 configuration file 이 있다.

  • c:\Program Files\MongoDB\Server\5.0\bin\mongod.cfg

이 파일의 bindIp 를 변경해주자. 그리고 mongo db 를 restart 한다.

...
# network interfaces
net:
  port: 27017
  # bindIp: 127.0.0.1
  bindIp: 0.0.0.0
...

접근 test

mongo client 설치

아래 경로는 http://repo.mongodb.org/apt/ubuntu/dists/ 를 들어가보고 확인하면 된다.

echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu $(lsb_release -cs)/mongodb-org/5.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list
sudo apt-get update
sudo apt list | grep mongo
sudo apt-get install -y mongodb-clients

mongo shell 실행

/usr/bin/mongo --shell 172.17.128.1/mydb

See Also

  1. 쿠…sal: [컴] wsl2 에서 windows 의 postgresql 에 접근하기

Reference

  1. 18.04 - Install MongoDB shell client without server - Ask Ubuntu

[컴] GoCD 에서 gradle build

 gocd에서 gradle build 하기 / ci/cd

GoCD 에서 gradle build

Releases page에서 jar을 download 할 수 있다. 이 jar을 $GO_SERVER_HOME/plugins/external 에 copy 해 넣고, 서버를 restart 하면 된다.

GoCD agent 에 JDK 설치

위의 plugin 을 설치하면 그냥 될 것이라 봤는데, 여전히 JAVA_HOME 설정이 필요하다. 즉, jdk 를 설치해야 한다. 그럴거면, 굳이 저 gradle plugin 을 쓸 필요가 있나 싶긴 하다.

여하튼 그래서 see also 1. 만드는 container 에서는 agent 가 jdk를 가지고 있지 않다. 그래서 아래처럼 Dockerfile 을 만들고, 기존 image 대신에 아래 Dockerfile 로 build 한 이미지를 쓰자.

FROM gocd/gocd-agent-alpine-3.14:v21.3.0
LABEL maintainer "i5on9i <gaedduck@duck.go>"

USER root
RUN  apk update \
  && apk upgrade \
  && apk add ca-certificates \
  && update-ca-certificates \
  && apk add --update coreutils && rm -rf /var/cache/apk/* \ 
  && apk add --update openjdk11 tzdata curl unzip bash \
  && apk add --no-cache nss \
  && rm -rf /var/cache/apk/*

ENTRYPOINT ["/docker-entrypoint.sh"]

USER go

compose.yml을 아래처럼 변경하고, docker compose up --build 을 하면 된다.

# compose.yml

version: "3"
services:
  server:
    container_name: gocd-server
    hostname: gocdserver
    ports:
      - 8153:8153
    image: gocd/gocd-server:v21.3.0
    volumes:
      - ./godata:/godata
    environment:
      - MSG_TIME=0
  agent:
    build: .
    # 이부분 대신에 build
    # image: gocd/gocd-agent-alpine-3.14:v21.3.0
    environment:
      - GO_SERVER_URL=http://gocdserver:8153/go
    depends_on:
      - server

networks:
  gocdnet:

See Also

  1. 쿠…sal: [컴] docker 를 사용해서 GoCD 실행하기

Reference

  1. GitHub - jmnarloch/gocd-gradle-plugin: GO Continuous Delivery Gradle plugin
  2. java - How to install oracle jdk11 in Alpine linux docker image? - Stack Overflow

[컴] GoCD 에서 id/pw 설정(authentication)

gocd authentication / auth 설정 /

GoCD 에서 id/pw 설정(authentication)

gocd/gocd-filebased-authentication-plugin 은 v17.5.0 이후로 GoCD에 통합되었다. 그래서 굳이 설치하지 않아도 된다.

여기서는 /godata/etc/password.properties 를 추가한다.

password file 생성

/godata/etc/password.properties 는 아래처럼 생겼다. <id>:<password> 로 된 password file 이다. 아래 값은 id ‘testuser’, password도’testuser’ 이다.

testuser:$2y$05$HhSO/CgnSwJ20.LDfk4zEO2XGtJX.FlTFN956zYiXGlsx3XZVoQcW

password file의 생성은 htpasswd 를 이용하는 것이 가장 빠를 듯 하다. windows 의 경우 binary 를 받아서 실행만 하면된다.

  • Generating passwords using htpasswd
  • htpasswd -c -B password_filename myuserid : 이러면, myuserid 라는 userid 가 추가된 file이 password_filename 라는 이름으로 만들어진다.

그밖의 메뉴

그밖의 메뉴는 직접 눌러보면 쉽게 이해할 수 있다.

See Also

  1. 쿠…sal: [컴] docker 를 사용해서 GoCD 실행하기