Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

b_9620 #97

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions craft/ci/craft-server/linux-aarch64/serverfiles/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,19 @@ ADD server.py /server.py
ADD world.py /world.py
ADD builder.py /builder.py
RUN gcc -std=c99 -O3 -fPIC -shared -o world -I src -I deps/noise deps/noise/noise.c src/world.c

RUN pip install requests boto3 psycopg2

#Install aws cli
RUN pip install awscli
RUN mkdir /root/.aws
COPY config /root/.aws

#Install kubectl
RUN curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/arm64/kubectl"
RUN chmod +x ./kubectl && \
mv kubectl /usr/sbin
RUN chmod 777 -R /usr/local/

ADD start.sh /start.sh
ADD monitor_active_game_sessions.sh /monitor_active_game_sessions.sh
4 changes: 4 additions & 0 deletions craft/ci/craft-server/linux-aarch64/serverfiles/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[default]
retry_mode = standard
max_attempts = 10
region = us-west-2
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/bash
touch /tmp/healthy
max_sessions_per_game_server=$MAX_SESSIONS_IN_GS
cooldown_after_scale_up=$SCALE_UP_COOLDOWN
is_cooled_down=0
while true
do
current_num_of_game_servers=$(kubectl get deploy $DEPLOY_NAME | grep -v NAME| awk '{print $3}')
num_of_active_sessions=$(netstat -anp | grep $(/sbin/ip addr| grep inet | grep -v inet6| grep -v 127.0.0.1| awk '{print $2}'| awk -F\/ '{print $1}') | grep 4080 | grep ESTABLISHED| wc -l)
echo num_of_active_sessions=$num_of_active_sessions
echo current_num_of_game_servers=$current_num_of_game_servers
if (( $num_of_active_sessions >= $max_sessions_per_game_server ))
then
if (( $is_cooled_down == 0 ))
then
echo "game server is at capacity; max_sessions_per_game_server=$max_sessions_per_game_server going to add more servers"
new_num_of_game_servers=$(echo $(( $current_num_of_game_servers + 1 )))
kubectl scale deploy $DEPLOY_NAME --replicas=$new_num_of_game_servers
echo cooling down for $cooldown_after_scale_up sec
sleep $cooldown_after_scale_up
is_cooled_down=1
else
echo "game server already scaled so removing it from the loadbalancer target group"
rm -f /tmp/healthy
fi
echo aws cloudwatch put-metric-data --metric-name num_of_game_servers --namespace craft --value $new_num_of_game_servers
aws cloudwatch put-metric-data --metric-name num_of_game_servers --namespace craft --value $new_num_of_game_servers
else
echo "game server has more free session slots, max_sessions_per_game_server=$max_sessions_per_game_server, num_of_active_sessions=$num_of_active_sessions"
echo aws cloudwatch put-metric-data --metric-name num_of_game_servers --namespace craft --value $current_num_of_game_servers
aws cloudwatch put-metric-data --metric-name num_of_game_servers --namespace craft --value $current_num_of_game_servers
is_cooled_down=0
fi

sleep 5
done
25 changes: 6 additions & 19 deletions craft/ci/craft-server/linux-aarch64/serverfiles/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
LOG_PATH = 'log.txt'

CHUNK_SIZE = 32
BUFFER_SIZE = 4096
#BUFFER_SIZE = 4096
BUFFER_SIZE = int(os.environ['BUFFER_SIZE'])
COMMIT_INTERVAL = 5

AUTH_REQUIRED = False
Expand Down Expand Up @@ -91,19 +92,14 @@ def execute_rds_read_statement(sql,param):
cursor = connection.cursor()
cursor.execute(sql,param)
rows = cursor.fetchall()
#print("execute_rds_read_statement rows=%s"%(rows))
#count = cursor.rowcount
#print(count, "Record inserted/updated successfully processed")
return rows
except (Exception, psycopg2.Error) as error:
print("Failed to insert/update", error)
sys.stdout.flush()
finally:
# closing database connection.
if connection:
cursor.close()
connection.close()
#print("PostgreSQL connection is closed")

def execute_rds_write_statement(sql,param):
try:
Expand All @@ -117,21 +113,16 @@ def execute_rds_write_statement(sql,param):

connection.commit()
count = cursor.rowcount
#print(count, "Record inserted/updated successfully processed")

except (Exception, psycopg2.Error) as error:
print("Failed to insert/update", error)
sys.stdout.flush()

finally:
# closing database connection.
if connection:
cursor.close()
connection.close()
#print("PostgreSQL connection is closed")

def execute_rds_statement_data_api(sql, sql_parameters=[]):
log('execute_rds_statement_data_api',sql,sql_parameters)
#log('execute_rds_statement_data_api',sql,sql_parameters)
response = rds_client.execute_statement(
secretArn=secret_arn,
database=database_name,
Expand Down Expand Up @@ -201,7 +192,7 @@ def handle(self):
if not data:
break
buf.extend(data.replace('\r\n', '\n'))
#print('self.set_block(%s)'%(data))
print('server Handle=%s'%(data))
sys.stdout.flush()
while '\n' in buf:
index = buf.index('\n')
Expand Down Expand Up @@ -377,7 +368,6 @@ def get_block(self, x, y, z):
rows = list(execute_rds_read_statement(sql,params))
#log('rds_response get_block',result['records'])
if rows:
log('rows from sqlite',rows[0][0])
return rows[0][0]
return self.get_default_block(x, y, z)
def next_client_id(self):
Expand Down Expand Up @@ -408,7 +398,6 @@ def on_data(self, client, data):
func = self.commands[command]
func(client, *args)
def on_disconnect(self, client):
#log('DISC', client.client_id, *client.client_address)
self.clients.remove(client)
self.send_disconnect(client)
#self.send_talk('%s has disconnected from the server.' % client.nick)
Expand All @@ -420,7 +409,6 @@ def on_version(self, client, version):
client.stop()
return
client.version = version
# TODO: client.start() here
def on_authenticate(self, client, username, access_token):
user_id = None
if username and access_token:
Expand All @@ -439,7 +427,6 @@ def on_authenticate(self, client, username, access_token):
else:
client.nick = username
self.send_nick(client)
# TODO: has left message if was already authenticated
self.send_talk('%s has joined the game.' % client.nick)
#agones_allocate(self)

Expand Down Expand Up @@ -480,7 +467,7 @@ def on_chunk(self, client, p, q, key=0):
# packets.append(packet(BLOCK, p, q, x, y, z, w))
# max_rowid = max(max_rowid, rowid)
for rowid, x, y, z, w in rows:
log('on_chunk-rows',x,y,z,w)
#log('on_chunk-rows',x,y,z,w)
blocks += 1
packets.append(packet(BLOCK, p, q, x, y, z, w))
max_rowid = max(max_rowid, rowid)
Expand Down Expand Up @@ -566,7 +553,7 @@ def on_block(self, client, x, y, z, w):
sql = 'insert into block (p, q, x, y, z, w) values (:p, :q, :x, :y, :z, :w) on conflict on constraint unique_block_pqxyz do UPDATE SET w = :w'
sql = """insert into block (p, q, x, y, z, w) values (%s,%s,%s,%s,%s,%s) on conflict on constraint unique_block_pqxyz do UPDATE SET w =%s"""
params=[p,q,x,y,z,w,w]
log('p=%d,q=%d,x=%d,y=%d,z=%d,w=%d'%(p,q,x,y,z,w))
log('insert into block - p=%d,q=%d,x=%d,y=%d,z=%d,w=%d'%(p,q,x,y,z,w))
#postgres_insert_query = """ INSERT INTO light (p,q,x,y,z,w) VALUES (%s,%s,%s,%s,%s,%s)"""
#record_to_insert = (1,2,3,4,5,6)
#response = execute_rds_statement(sql, sql_parameters)
Expand Down
4 changes: 4 additions & 0 deletions craft/ci/craft-server/linux-aarch64/serverfiles/start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

/monitor_active_game_sessions.sh&
python /server.py
4 changes: 2 additions & 2 deletions craft/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def execute_rds_read_statement(sql,param):
finally:
if connection:
cursor.close()
connection.close()
connection.close()

def execute_rds_write_statement(sql,param):
try:
Expand Down Expand Up @@ -193,7 +193,7 @@ def handle(self):
if not data:
break
buf.extend(data.replace('\r\n', '\n'))
#print('self.set_block(%s)'%(data))
print('server Handle=%s'%(data))
sys.stdout.flush()
while '\n' in buf:
index = buf.index('\n')
Expand Down