<commit_before>import rpc, logging
import messages_pb2 as msg
import guid as Guid
import mds
import gevent.server
from pylvm2.lvm_ctrl import *
from pytgt.tgt_ctrl import *
import util 
import random

MDS_IP='192.168.0.149'
MDS_PORT=2345
CHK_IP='192.168.0.149'
CHK_PORT=6789
VGNAME='VolGroup'
LVNAME='lv_softsan_'


class ChunkServer:

	def __init__(self):
		self.tgt = Tgt()
		self.lvm = LVM2()
		self.tgt.reload()
		self.lvm.reload()
	
# chkserv always uses lun_index=1. if the requested volume does notchkserv 
	def AssembleVolume(self, req):
		ret = msg.AssembleVolume_Response()
		a_guid = req.volume.guid
		target_id = 1		
		while self.tgt.is_in_targetlist(target_id):
			target_id = str(random.randint(0,1024*1024))
		target_name = "iqn:softsan_"+Guid.toStr(a_guid)
		acl = 'ALL'
		lun_index = '1'
		lv_name = LVNAME+Guid.toStr(a_guid)
		lun_path = '/dev/'+VGNAME+'/'+lv_name
		if self.tgt.assemble(target_id, target_name, lun_path, acl)!=None:
			ret.error = 'Failed to assemble volume'
			return ret
		self.tgt.reload()			
		ret.access_point = target_name
		return ret
		
# why DisassembleVolume_Response return the value of access_point? keep consistent with request
	def DisassembleVolume(self, req):
		ret = msg.DisassembleVolume_Response()
		print 'in DisassembleVolume()', req.access_point
		self.tgt.reload()
		target_name = req.access_point
		# print 'target_name', target_name 
		target_id = self.tgt.target_name2target_id(target_name)
		# print 'target_id:',target_id
		if target_id==None:
			ret.error='No such access_point'
			return ret
		if self.tgt.delete_target(target_id)!=None:
			ret.error='failed to unbind target'
		return ret

# try to create every requested chunk.however, if some chunk can not be created, fill the ret.error with the output of lvcreate 
	def NewChunk(self, req):
		ret = msg.NewChunk_Response()
		size = str(req.size)+'M'
		for i in range(req.count):
			a_guid = Guid.generate()
			lv_name = LVNAME+Guid.toStr(a_guid)
			lv_size = size
			output =  self.lvm.lv_create(VGNAME, lv_name, lv_size)
			if output == None:
				t=ret.guids.add()
				Guid.assign(t, a_guid)
				key=Guid.toTuple(a_guid)
			else: 
				ret.error = str(i) + ':' + output + ' '
				continue
		self.lvm.reload()
		return ret

# try to delete every requested chunk. if it can not delete, fill the ret.error with output of lvremove
	def DeleteChunk(self, req):
		ret = msg.DeleteChunk_Response()
		for a_guid in req.guids:
			lv_name = LVNAME+Guid.toStr(a_guid)
			lv_path = '/dev/'+VGNAME+'/'+lv_name
			output = self.lvm.lv_remove(lv_path)
			if output!=None:
				error += Guid.toStr(a_guid)+':'+output+' '
				ret.error += error
		self.lvm.reload()
		return ret

# untested function, it did not fill all the attribute of chunkserverInfo
def heartBeat(server):
	global guid
	socket=gevent.socket.socket()
	socket.connect((MDS_IP, MDS_PORT))
	stub=rpc.RpcStub(guid, socket, mds.MDS)
	(myip, myport)=socket.getsockname()
	while True:
		# print 'calling ChunkServerInfo'
		info=msg.ChunkServerInfo()
		info.ServiceAddress=CHK_IP<commit_msg>minor fix<commit_after>import rpc, logging
import messages_pb2 as msg
import guid as Guid
import mds
import gevent.server
from pylvm2.lvm_ctrl import *
from pytgt.tgt_ctrl import *
import util 
import random

MDS_IP='192.168.0.149'
MDS_PORT=2345
CHK_IP='192.168.0.149'
CHK_PORT=6789
VGNAME='VolGroup'
LVNAME='lv_softsan_'

class ChunkServer:

	def __init__(self):
		self.tgt = Tgt()
		self.lvm = LVM2()
		self.tgt.reload()
		self.lvm.reload()
	
#  always use lun_index=1. 
	def AssembleVolume(self, req):
		self.tgt.reload()
		ret = msg.AssembleVolume_Response()
		a_guid = req.volume.guid
		while True:
			target_id = str(random.randint(0,1024*1024))
			if not self.tgt.is_in_targetlist(target_id): 
				break			
		target_name = "iqn:softsan_"+Guid.toStr(a_guid)
		find_target = self.tgt.target_name2target_id(target_name)
		if find_target != None:
			ret.access_point = target_name
		else:
			lv_name = LVNAME+Guid.toStr(a_guid)
			lun_path = '/dev/'+VGNAME+'/'+lv_name
			if self.tgt.assemble(target_id, target_name, lun_path, 'ALL')!=None:
				ret.error = 'Failed to assemble volume'
				return ret
			ret.access_point = target_name
		return ret
		
	def DisassembleVolume(self, req):
		self.tgt.reload()
		ret = msg.DisassembleVolume_Response()
		logging.debug('in DisassembleVolume()', req.access_point)
		target_name = req.access_point
		target_id = self.tgt.target_name2target_id(target_name)
		if target_id==None:
			ret.error='No such access_point'
			return ret
		if self.tgt.delete_target(target_id)!=None:
			ret.error='failed to unbind target'
		return ret

# try to create every requested chunk.however, if some chunk can not be created, fill the ret.error with the output of lvcreate 
	def NewChunk(self, req):
		self.lvm.reload()
		ret = msg.NewChunk_Response()
		size = str(req.size)+'M'
		for i in range(req.count):
			a_guid = Guid.generate()
			lv_name = LVNAME+Guid.toStr(a_guid)
			lv_size = size
			output =  self.lvm.lv_create(VGNAME, lv_name, lv_size)
			if output == None:
				t=ret.guids.add()
				Guid.assign(t, a_guid)
				key=Guid.toTuple(a_guid)
			else: 
				ret.error = str(i) + ':' + output + ' '
		return ret

# try to delete every requested chunk. if it can not delete, fill the ret.error with output of lvremove
	def DeleteChunk(self, req):
		self.lvm.reload()
		ret = msg.DeleteChunk_Response()
		for a_guid in req.guids:
			lv_name = LVNAME+Guid.toStr(a_guid)
			lv_path = '/dev/'+VGNAME+'/'+lv_name
			output = self.lvm.lv_remove(lv_path)
			if output!=None:
				error += Guid.toStr(a_guid) + ':'+ output+ ' '
				ret.error += error
		return ret

def heartBeat(server):
	global guid
	socket=gevent.socket.socket()
	socket.connect((MDS_IP, MDS_PORT))
	stub=rpc.RpcStub(guid, socket, mds.MDS)
	(myip, myport)=socket.getsockname()
	while True:
		info=msg.ChunkServerInfo()
		info.ServiceAddress=CHK_IP
{'fraction_non_alphanumeric': 0.11878499915153572, 'fraction_numerical': 0.014763278465976583, 'mean_word_length': 4.179261862917399, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6365827', 'n_tokens_mistral': 2500, 'n_tokens_neox': 2224, 'n_words': 498}