TCP UDP 버퍼 길이 확인

Linux 2014. 10. 8. 10:29

특정 기능을 개발하다 보면,

TCP나 UDP의 버퍼길이까지 튜닝하여야 하는 경우가 발생한다.  (예: heartbeat 데몬 등)


이 때 TCP/UDP의 버퍼길이 조정을 하는 api는 주로 setsockopt를 통해 하게 되는데

min이나 max가 제한되어 있으므로 이를 미리 확인하는게 좋다.


Linux에서


/sbin/sysctl -a   | grep net.core 와

/sbin/sysctl -a   | grep net.ipv4 로 확인이 가능하다.



//아래 예제에서 wmem:sendBuf,  rmem: receive Buf

net.core.wmem_max = 5242880   

net.core.rmem_max = 5242880

net.core.wmem_default = 2097152

net.core.rmem_default = 2097152


net.ipv4.tcp_mem = 196608       262144  393216

net.ipv4.tcp_wmem = 16384       122880  204800

net.ipv4.tcp_rmem = 16384       122880  204800


net.ipv4.udp_mem = 761952       1015936 1523904

net.ipv4.udp_rmem_min = 2097152

net.ipv4.udp_wmem_min = 2097152




아직 테스트 중이라 확실치 않은 부분이 있긴한데

setsockopt 함수를 사용하면 sendbuf는 2048, receiveBuf는 256까지 min으로 설정이 되는 것으로 보인다.

 (설정값 *2 로 세팅이 되므로 함수 호출시에는 그 반으로 해야함) 

Posted by yongary
,

redis를 사용하는 유명회사들에 대해선

redis.io 웹사이트에 다음과 같은 회사들이 언급되어 있다.


Blizzard에서 Wow 아바타 서비스에도 사용한다고 하는데 아직 확실치는 않다..

Posted by yongary
,

SW Engineering

IT 2014. 9. 29. 08:54

소프트웨어 공학의 최근 흐름으로는 관점 지향(Aspect), 애자일(Agile), 모델 주도(Model-Driven) 등이 있다.



Agile 방법론의 기본 개념 "반복적으로 프로토 타입을 고객에 전달함으로써 고객의 요구사항 변화에 민첩하게 대응한다"


Agile 방법론의 대표주자XP(익스트림 프로그래밍) 




XP(익스트림 프로그래밍) 개발 프로세스 :


XP의 목적은 '고객이 원하는 양질의 소프트웨어를 빠른 시간안에 전달하는 것'이다. 
수시로 발생하는 고객의 요구사항에 대처하고, 고객이 원하는 SW를 고객이 원하는 시간에 인도하기 위해서는 고객과 팀원간의 대화를 통해 해결한다.


XP에는 7가지 항목이 정의되어 있는데, 7가지 항목 중 특히 공감이 가는 항목은 아래와 같이 4항목이다.

1.Simple-Design

2.Test-Driven

Test_driven은 다른 애자일 방법론과 구분되는 XP만의 특징이기도 하다.

프로그래머들이 코딩을 할 때에, 테스트 코드를 작성하도록 함과 동시에 테스트를 기반으로 프로젝트를 완성시켜 나간다.

3. Pair-Prograimming 

두명 혹은 그 이상의 프로그래머가 함께 코딩을 하는 것을 말한다. 두명의 프로그래머가 함께 코딩을 하고 테스트를 통해서 개발을 할 수도 있고, 한명은 코딩을 하고 한명은 Quality Assurance 역할 통해서 테스트에만 집중을 할 수도 있다.

4. Whole-Team 

기획자, 설계자 심지어는 Tester, User까지도 하나의 팀이 되어 개발을 진행하는 방법이다.


나머지 3항목은 

Planning Game, Small Release, Customer Test이다.








Posted by yongary
,

Linux 버전 확인

Linux 2014. 9. 26. 16:50

Linux kernel 버전 확인:  uname -a    
                                 or  lsb_release -a


OS 버전 확인:

redhat계열:  cat /etc/redhad-release

ubuntu 계열:  cat /etc/issue

Mac: sw_vers

 

32 / 64 bit 확인 :

$ getconf LONG_BIT 

 


 


 

Posted by yongary
,

vim 탭설정

Linux 2014. 9. 25. 10:43

vim 에디터가 요즘 대세인데. 탭 기능이 다소 불편할 수 있다.


Vim에서는 Ctrl+Tab단축키(혹은 linux에서 Ctrl+Shift+Tab단축키)가  기본적으로는 분할된 (:sp 명령어로) 화면간 전환에 쓰이기 때문인데..



개인적으로 애용하는 방법은

.vimrc 파일에  아래와 같이 3줄 추가하는 것이다.


map  <C-l> :tabn<CR>

map  <C-h> :tabp<CR>

map  <C-n> :tabnew<CR>



Windows의 경우에는 

C:\Program Files\Vim\_vimrc 파일에 추가.

 

 

기타 특수명령어

 Shift + j   :  라인구분(엔터키)가 하나씩 없어진다

set list  / set unlist  :  특수키를 다 구경할 수 있다.

Posted by yongary
,

redis를 HA로 구성했을 경우, SDR(spring-data-redis 1.4) 와 jedis커넥션을 이용한 spring과 redis 연동.


RedisSentinelConfiguration 클래스를 이용하면, HA를 지원하는 redis의 이중화 상태에서도 spring을 이용한 redis연동이 가능하다.

java코드로 연동하는 방법은 여러곳에서 나와있으므로, 본 글 에서는

xml을 이용한 DI(Dependency Injection)방식으로   spring과 HA redis를 연동하는 방식을 설명한다.



1. pom.xml 수정   (SDR , jedis를 추가)

이전글과 동일하다. 단 common-pool 대신에 이를 상속받은 JedisPoolConfig을 써봄으로 인해 common-pool은 제거.


<dependency>

<groupId>org.springframework.data</groupId>

<artifactId>spring-data-redis</artifactId>

<version>1.4.0.RELEASE</version>

</dependency>


<dependency>

<groupId>redis.clients</groupId>

<artifactId>jedis</artifactId>

<version>2.5.1</version>

<type>jar</type>

<scope>compile</scope>

</dependency>




2. application-config.xml 수정


2.1 네임스페이스에  xmlns:p,c, util 추가.


2.2.1 mymaster와 setntinels을 xml로 구성


2.2.2 RedisSentinelContiguration을 이용해서 HA구성.

         => SDR에 미흡한 부분이 하나 존재하여, MySentinelConfiguration  를 별도로 생성해서 이용하도록 함.

             (이유는 아래 xml의 코멘트 참조)   


2.2.3 jedis를 이용한 Pool과 connection구성.


이렇게만 하면 이전 예제와 동일하게 그냥 redisTemplate만을 이용해서 개발하면 자동으로 HA구성을 지원하는 개발이 된다.




<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:p="http://www.springframework.org/schema/p"

xmlns:c="http://www.springframework.org/schema/c"

xmlns:util="http://www.springframework.org/schema/util"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd

http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">


<!-- Uncomment and add your base-package here:-->

<context:component-scan base-package="my"/>


<!-- myster -->

<bean id="myMaster" class= "org.springframework.data.redis.connection.RedisNode" c:host="127.0.0.1" c:port="6379" p:name="mymaster"/>


<!-- Sentinels -->

<bean id="mySent1" class= "org.springframework.data.redis.connection.RedisNode" c:host="127.0.0.1" c:port="26379"/>

<bean id="mySent2" class= "org.springframework.data.redis.connection.RedisNode" c:host="127.0.0.1" c:port="26381"/>

<bean id="mySents" class= "java.util.HashSet">

 <constructor-arg>

  <list>

   <ref bean="mySent1" />

   <ref bean="mySent2" />

  </list>

 </constructor-arg>

</bean>


<!-- RedisSentinelConfiguration: org.springframework.data.redis.connection.RedisSentinelConfiguration" 

2.2.2 이 부분이 유일하게 아직 SDR에서 미흡한 부분이다.  이 부분은 ReidsSentinelConfiguration class 가 

getter와 setter 파라미터가 다른 오류가 발생하므로, 자체 class를 하나 만들어서 해결하였다.

-->

<bean id="redisSentinelConf" class="my.MySentinelConfiguration"

p:master-ref="myMaster"

p:mySentinels-ref="mySents"

/>

<!-- p:sentinels-ref="mySents" : ERROR 발생-->



<!--2.2.3 POOL & Connection-->

<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"/>

<bean id="jedisConnFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"

    p:use-pool="true"

    p:poolConfig-ref="jedisPoolConfig"

    c:sentinelConfig-ref="redisSentinelConf"

/>

<!--p:host-name="127.0.0.1" p:port="6379" -->



<bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/>

<!-- redis template definition -->

<bean id="redisTemplate"

class="org.springframework.data.redis.core.RedisTemplate"

p:connection-factory-ref="jedisConnFactory"

p:keySerializer-ref="stringRedisSerializer"

p:hashKeySerializer-ref="stringRedisSerializer"

p:valueSerializer-ref="stringRedisSerializer"/>

</beans>





3. JAVA  코드 - 유일하게 추가한 MySentinelConfiguration코드이다.  추가만 해 놓으면 된다.


- 단지, SDR의 getter와 setter의 오류를 피하기 위한 클래스이다. 향후에는 SDR에서 개선이 될 것으로 기대된다.


public class MySentinelConfiguration extends RedisSentinelConfiguration {

MySentinelConfiguration(){

super();

}


public Set<RedisNode> getMySentinels(){

Set<RedisNode> sets=super.getSentinels();

return sets;

}


public void setMySentinels( Set<RedisNode> sentinels){

super.setSentinels(sentinels);

}


}


Posted by yongary
,

spring과 redis연동

Spring 2014. 9. 18. 09:26

SDR(spring-data-redis) 와 jedis커넥션을 이용한 spring을 이용한 redis 연동.


spring에서 redis를 연결하기 위한 기본적인 방법을 커넥션 위주로 설명.

실제로는 redis에 저장할 데이타 class를 별도로 만들어 serialize기능을 이용해 {key,class}형태로 저장하는 방식이 바람직하지만, 여기서는 이 부분은 생략하고 기본적인 연결과정만 설명한다. 


1. pom.xml 수정   (SDR , common-pool,  jedis를 추가)


<dependency>

<groupId>org.springframework.data</groupId>

<artifactId>spring-data-redis</artifactId>

<version>1.4.0.RELEASE</version>

</dependency>

<dependency>

<groupId>commons-pool</groupId>

<artifactId>commons-pool</artifactId>

<version>1.6</version>

</dependency>

<dependency>

<groupId>org.apache.commons</groupId>

<artifactId>commons-pool2</artifactId>

<version>2.0</version>

</dependency>

<dependency>

<groupId>redis.clients</groupId>

<artifactId>jedis</artifactId>

<version>2.5.1</version>

<type>jar</type>

<scope>compile</scope>

</dependency>






2. application-config.xml 수정


2.1 네임스페이스에  xmlns:p 추가.

2.2  jedisConnnection, Serializer, redisTemplate 추가.


<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:p="http://www.springframework.org/schema/p"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">


    <!-- Uncomment and add your base-package here:-->

    <context:component-scan base-package="my"/>


<bean id="jedisConnFactory"

    class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"

    p:use-pool="true" p:host-name="127.0.0.1" p:port="6379" />


<bean id="stringRedisSerializer"

    class="org.springframework.data.redis.serializer.StringRedisSerializer"/>


<!-- redis template definition -->

<bean id="redisTemplate"

class="org.springframework.data.redis.core.RedisTemplate"

p:connection-factory-ref="jedisConnFactory"

p:keySerializer-ref="stringRedisSerializer"

p:hashKeySerializer-ref="stringRedisSerializer"

p:valueSerializer-ref="stringRedisSerializer"/>


</beans>



  이 중에서 serializer는 추가하지 않아도 동작은 잘 되지만, 

 기본 serializer로 동작하게 되면 redis에 데이타가 들어갈 때, 앞 부분에 xac xed x00 x05t x00 등의 character가 몇개 들어가게 된다.

(이런 이상한 캐릭터들은 standard serializer에서 사용하는 class정보이다.)




3. JAVA Service 코드


 @Service

public class RedisService implements IRedisService{


@Autowired

private RedisTemplate<String,String> redisTemplate;


public Set<String> keysAll() {


//값을 넣고 싶을 땐, 이렇게..

//redisTemplate.opsForValue().set("abcKey", "valueABC");


return redisTemplate.keys("*");

}



}     


Posted by yongary
,

redis 실행법

mongoDB, redis 2014. 9. 16. 17:49

redis 설치 완료 후, src 폴더로 이동해서


1. stand-alone 모드 실행.

./redis-server  로 서버실행

./redis-cli  로 client 실행.

==> stand alone 모드로 default세팅 되어 실행된다.


reds> info          하면 정보 조회가능(데이타 개수도 있음)

redis>  keys *     하면 모든키 조회 가능(데이타가 많을경우는 조심)

 1)"MPx_TYPE_Audiox"  

 2)"FS_HB"

 3)"MP_TYPE_CTRL"
 4)"XCOD_ROOM_x"

 5)"CNFR_ROOM_x"

 

 



2. 마스터-슬레이브 모드

만약, master-slave모드로 테스트를 해보고 싶으면  redis-server를 아래명령으로 하나 더 실행한다.


$>./redis-server --port 6380 --slaveof 127.0.0.1 6379    



혹시, 이 상태에서 상태를 확인해보고 싶으면 client창에서


127.0.0.1:6379>  info  를 입력하면

#Replication 영역에

role:master

connected_slaves:1

      임을  을 확인할 수 있다.  즉 6379서버의 slave가 잘 뜬 것이다. 




3. Sentinel감시를 통한 HA 구성


위의 1,2번이 된 상태에서  아래와 같이 sentinel을 2개 정도 실행시키고....

$> src/redis-server sentinel1.conf --sentinel

$> src/redis-server sentinel2.conf --sentinel

==> 이러면 기본적인 HA완성.


이 때 1번 서버를 kill등으로 죽이면, 절체(HA)가 발생한다.


절체발생 후, 

client는 새로이 아래와 같은 명령으로 접속한다. 즉 2번 서버로 접속한다. 

$> redis-cli -p 6380  



ㅁ 좀 더 쉽게 절체를 발생시키는 방법은

redis-cli -p 26379 로 센티널로 cli접속을 한 다음에

>  sentinel failover mymaster    를 입력하면 바로 절체가 발생한다. 


  ( http://redis.io/topics/sentinel  참조)

  







 

Posted by yongary
,

vi에디터 한글세팅

Linux 2014. 9. 16. 17:12

한글인코딩을 euc-kr로 할지, utf-8로 할지 고민이 되는 경우,

한국에서만 쓸 것이라면 둘 다 상관이 없고 euc-kr이 좀 더 나을 수도 있을 것 같고

혹시 외국에서도 쓸지 모른다는 생각이 든다면 utf-8이 좋을 것 같다.


대세는 utf-8이라고 보고.. utf-8로 세팅하는 방법을 보면 다음과 같다.


$> env  혹은 locale 을 해보면  os의 LANG을 알 수 있다.

대부분 xx_xx.utf-8 로 되어있다면 문제가 없다.


1. vi 에디터에서 파일변환

:set fileencodings=utf-8  후에 :w 하고 다시 시작.



2. 프롬프트 상에서 파일변환은

iconv  -f euc-kr -t utf-8   euc-kr.html > utf8.html



혹시 

3. vi에디터에서 안보이면

:set enc=utf-8 




참고:   http://www.technet.co.kr/bbs/board.php?bo_table=tip&wr_id=263&page=3 

인코딩 설명:  http://helloworld.naver.com/helloworld/19187 

Posted by yongary
,

< 함수의 실행시간을 milisecond단위로 측정하는 방법. > 



struct timeval을 이용하여 함수의 실행시간을 측정하는 방법인데,


 struct timeval은 초와 micro초로 이루어져 있으므로,

- 초에는 *1000을 

 - micro초에는 /1000 을 해야 milisecond가 된다.


struct timeval { long tv_sec; // 초 long tv_usec; // micro }



=======사용예제================================================

#include <sys/time.h>


struct timeval before,after;  //선언


//사용 
    gettimeofday(&before,NULL);
    run_my_function( );
    gettimeofday(&after,NULL);

    printf("function running time[ms] : %ld\n",
         1000*(after.tv_sec-before.tv_sec) +   
         (after.tv_usec-before.tv_usec)/1000);





Posted by yongary
,