Oracle11gR2——RAC中的服务

1、关于Oracle Service

To manage workloads or a group of applications, you can define services that you assign to a particular application or to a subset of an application's operations. You can
also group work by type under services. For example, online users can use one service, while batch processing can use another and reporting can use yet another service to
connect to the database.


Oracle recommends that all users who share a service have the same service level requirements. You can define specific characteristics for services and each service can represent a separate unit of work. There are many options that you can take advantage of when using services. Although you do not have to implement these options, using them helps optimize application performance.

也就是说我们可以为特定的应用或者应用操作集合定义服务,例如为联机事务处理,批处理和报表应用分别定义服务。然后通过这些服务来连接到数据库。使用服务的原因在于可以通过对服务进行配置,获得诸如应用程序性能,负载,以及可用性等各个方面的好处。

2、Service的几个重要的配置

2.1 Service Management Policy

service的startup设置,如果将一个service的management policy配置为AUTOMATIC,则在使用srvctl启动database的时候,服务也自动启动。

如果将management policy设置为MANUAL,则必须使用srvctl来启动服务。

使用crsctl重启集群时,如果服务之前是运行的,那么服务也将重新启动。

2.2 Instance Preference

一个服务的优选实例是将在其中启动该服务的实例,这些实例将为用户服务。可用实例是备用实例,当优选实例发证故障时,将在这些实例上启动服务。

假设为一个服务定义了2个优选实例,那么集群会尽量确保服务始终运行在2个实例上。

当服务运行在可用实例上时,当优选实例恢复完成,服务不会在再次回到优选实例上。

注意优选实例和可用实例的定义只有在数据库为administrator-managed的方式下才能设置。如果database处于policy-managed的方式下,则应当为服务指定server pool。见下一节。

2.3 server pool

When you define services for a policy-managed database, you assign the service to a
server pool where the database is running using SRVCTL with the -g option. You can define the service as either UNIFORM (running on all instances in the server pool) or
SINGLETON (running on only one instance in the server pool) using the -c option. For singleton services, Oracle RAC chooses on which instance in the server pool the
service is active. If that instance fails, then the service fails over to another instance in the server pool. A service can only run in one server pool.

server pool相当于虚拟实例组,它包含一定数量的实例,而不指定具体是哪些实例。可以定义服务在一个server pool中的所有实例上运行或者在server pool中的一个实例中运行。

3、Oracle中与Service集成的功能

3.1 Resource Profile for a Service

resource profile在定义service时自动创建。它描述了集群如何管理service以及当优选实例故障时service将转移到哪个实例。resource profile还定义了服务的依赖关系,保证在数据库 停止时,实例与服务可以自动以正确的顺序停止。


3.2 Database Resource Manager Consumer Group Mappings for Service

service可以和Resource Manager Consumer Group集成,一个service属于一个Resource Manager Consumer Group。通过Resource Manager Consumer Group可以定义服务对资源的使用优先级。例如可以限定三个Group的优先级及CPU使用率,那么CPU资源将按优先级分配给各个组。

3.3 AWR for Service

可以通过AWR报告监控service性能。

4、创建服务

文档中创建服务的语法和选项如下:

srvctl add service -d db_unique_name -s service_name {-r "preferred_list"
[-a "available_list"] [-P {BASIC | NONE | PRECONNECT}] | -g server_pool
[-c {UNIFORM | SINGLETON]} [-k network_number]
[-l [PRIMARY | PHYSICAL_STANDBY | LOGICAL_STANDBY | SNAPSHOT_STANDBY]
[-y {AUTOMATIC | MANUAL}] [-q {TRUE | FALSE}] [-x {TRUE | FALSE}]
[-j {SHORT | LONG}][-B {NONE | SERVICE_TIME | THROUGHPUT}]
[-e {NONE | SESSION | SELECT}] [-m {NONE | BASIC}] [-z failover_retries]
[-w failover_delay]

-d db_unique_name Unique name for the database.

-s service_name The service_name.service_domain should be unique within the cluster unless you want to spread
connections across multiple databases that offer the same
service. If you do not specify the service domain as part of the service name (such as sales.foo.com), then the
DB_DOMAIN database attribute is the default.

-r preferred_list A list of preferred instances on which the service runs when the database is administrator managed.
The list of preferred instances must be mutually exclusive
with the list of available instances.
Note: This option is available only with Oracle RAC and
only for administrator-managed databases.

-a available_list A list of available instances to which the service fails over when the database is administrator managed.
The list of available instances must be mutually exclusive
with the list of preferred instances.
Note: This option is available only with Oracle RAC and
only for administrator-managed databases.

-P {BASIC | NONE | PRECONNECT} TAF policy specification (for administrator-managed
databases only).
Notes:
■ You can only use PRECONNECT when you specify the -r and -a options.
■ For administrator-managed databases, you cannot set
the client-side TAF policy to PRECONNECT.

-g server_pool The name of a server pool used when the database is
policy managed.
Note: This option is available only with Oracle RAC and
only for policy-managed databases.

-c {UNIFORM | SINGLETON} The cardinality of the service, either UNIFORM (offered on
all instances in the server pool) or SINGLETON (runs on
only one instance at a time).
Notes:
■ This option is available only with Oracle RAC and
only for policy-managed databases.
■ For policy-managed Oracle RAC One Node
databases, all services must be SINGLETON.

-k network_number Use this option to determine on which network this service is offered. The service is configured to depend on
VIPs from the specified network.
Note: This option is available only with Oracle RAC and
Oracle RAC One Node database configurations.

-l {[PRIMARY] |
[PHYSICAL_STANDBY] |
[LOGICAL_STANDBY] |
[SNAPSHOT_STANDBY]}
The service role.
You use this option to indicate that the service should only
be automatically started when the Oracle Data Guard
database role matches one of the specified service roles.
See Also: Oracle Data Guard Concepts and Administration
for more information about database roles

-y {AUTOMATIC | MANUAL} Service management policy.
If AUTOMATIC (the default), then the service is
automatically started upon restart of the database, either
by a planned restart (with SRVCTL) or after a failure.
Automatic restart is also subject to the service role,
however (the -l option).
If MANUAL, then the service is never automatically
restarted upon planned restart of the database (with
SRVCTL). A MANUAL setting does not prevent Oracle
Clusterware from monitoring the service when it is running and restarting it if a failure occurs.
Note: Using CRSCTL to stop and start the Oracle Clusterware restarts the service in the same way that a
failure does.

-q {TRUE | FALSE} Indicates whether AQ HA notifications should be enabled
(TRUE) for this service.

-x {TRUE | FALSE} Indicates whether or not Distributed Transaction
Processing should be enabled for this service. This service
will either be a singleton service in a policy-managed
database or a preferred service on a single node in an
administrator-managed database.
Note: This option is available only with Oracle RAC.

-j {SHORT | LONG} Assign a connection load balancing goal to the service: SHORT if using an integrated connection pool, LONG for
long running connections that you want balanced by the
number of sessions per node for the service

-B {NONE | SERVICE_TIME |
THROUGHPUT}
Goal for the Load Balancing Advisory.

-e {NONE | SESSION | SELECT} Failover type.

-m {NONE | BASIC} Failover method.
If the failover type (-e) is set to a value other than NONE,
then you should choose BASIC for this option.
Note: This option is available only with Oracle RAC.

-z failover_retries The number of failover retry attempts.

-w failover_delay The time delay between failover attempts.

-u Add a new preferred or available instance to an existing
service configuration.

-f Force the add operation even though a listener is not
configured for the network.


5、关于服务在实例中切换的实验

测试环境OGI和RDBMS都是11.2.0.1版本,数据库有两个节点node1,node2,对应实例为orarac1,orarac2

该服务使用管理员管理方式,首选实例和可用实例相关配置如下:

[oracle@node1 ~]$ srvctl config service -d orarac -s test_service
服务名: test_service
服务已启用
服务器池: orarac_test_service
基数: 1
断开连接: 假
服务角色: PRIMARY
管理策略: AUTOMATIC
DTP 事务处理: 假
AQ HA 通知: 假
故障转移类型: SELECT
故障转移方法: BASIC
TAF 故障转移重试次数: 0
TAF 故障转移延迟: 0
连接负载平衡目标: LONG
运行时负载平衡目标: NONE
TAF 策略规范: BASIC
首选实例: orarac2
可用实例: orarac1

服务的运行状态服下:

[[oracle@node1 ~]$ srvctl status instance -d orarac -i orarac1
实例 orarac1 正在节点 node1 上运行
[oracle@node1 ~]$ srvctl status instance -d orarac -i orarac2
实例 orarac2 正在节点 node2 上运行
[oracle@node1 ~]$ srvctl status service -d orarac -s test_service
服务 test_service 正在实例 orarac2 上运行

由于首选实例设置为orarac2,所以服务默认随实例orarac2自动启动。

使用srvctl关闭实例2,在查看服务的情况:

[oracle@node1 ~]$ srvctl stop instance -d orarac -i orarac2
[oracle@node1 ~]$ srvctl status service -d orarac -s test_service
服务 test_service 未运行。

使用srvctl关闭首选实例,则服务并未转移到可用实例上。

再看使用sqlplus关闭实例2的情况:

首先开启实例2,服务又随实例自动启动了

[oracle@node1 ~]$ srvctl start instance -d orarac -i orarac2
[oracle@node1 ~]$ srvctl status service -d orarac -s test_service
服务 test_service 正在实例 orarac2 上运行

在节点2上登入sqlplus关闭实例2

SQL> select instance_name from v$instance;

INSTANCE_NAME
------------------------------------------------
orarac2

SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.

再查看服务状态:

[oracle@node1 ~]$ srvctl status service -d orarac -s test_service
服务 test_service 正在实例 orarac1 上运行

服务已经转移到备选实例上了。

上面的实验结果表明:
服务会随srvctl启动实例而启动,服务不会随srvctl关闭实例而转移,服务会随sqlplus关闭实例而转移。

再看看服务是否会随sqlplus启动实例而启动:

[oracle@node1 ~]$ srvctl stop service -d orarac -s test_service
[oracle@node1 ~]$ srvctl status instance -d orarac -i orarac1
实例 orarac1 正在节点 node1 上运行
[oracle@node1 ~]$ srvctl status instance -d orarac -i orarac2
实例 orarac2 没有在 node2 节点上运行
[oracle@node1 ~]$ srvctl status service -d orarac -s test_service
服务 test_service 未运行。

node2上通过sqlplus启动实例:

INSTANCE_NAME
------------------------------------------------
orarac2

SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup
ORACLE instance started.

Total System Global Area 1603411968 bytes
Fixed Size                  2213776 bytes
Variable Size            1207961712 bytes
Database Buffers          385875968 bytes
Redo Buffers                7360512 bytes
Database mounted.
Database opened.

查看服务有没有随SQLPLUS启动实例而启动:

[oracle@node1 ~]$ srvctl status service -d orarac -s test_service
服务 test_service 未运行。

结论:
服务会随srvctl启动实例而启动,服务不会随srvctl关闭实例而转移。
服务不会随sqlplus中startup启动实例而启动,服务会随sqlplus关闭实例而转移。
注:测试环境OGI与RDBMS版本都是11.2.0.1。

6、关于RAC中的故障转移

6.1 关于RAC中的故障转移

6.1.1 客户端的故障转移

在客户端的tns中配置多个VIP连接,客户端连接时依次尝试连接,直至连接成功,如果连接中故障,则需要重新连接。

tnsnames.ora中有一个参数FAILOVER=ON,这个参数默认为ON所以不需要配置,一个tns条目可能如下:

ORARAC =
  (DESCRIPTION =
     (ADDRESS = (PROTOCOL = TCP)(HOST = orarac1-vip)(PORT = 1521))
     (ADDRESS = (PROTOCOL = TCP)(HOST = orarac2-vip)(PORT = 1521))
     (LOAD_BALANCE=YES)
      (
 CONNECT_DATA=
     (SERVER=DEDICATED)
 (SERVICE_NAME=orarac)
      )
    )
    
6.1.2 TAF

由于大部分应用使用长连接,即只创建一次到oracle的连接,以后都使用这个连接,如果用客户端故障转移必须要重新建立连接。为解决这个问题,需要新的故障转移方式TAF。

TAF也是在tnsnames.ora中定义。其包含METHOD及TYPE选项。

METHOD取值有BASIC和PRECONNECT两种:BASIC为发生故障时立刻切换。PRECONNECT为事先创建冗余连接。

TYPE取值有SESSION和SELECT两种:对于正在查询的语句,如果实例故障,SESSION模式需要重新查询,SELECT模式可以继续返回结果集,用户感受不到故障转移。

一个TAF的tnsnames中的配置可能如下:

ORARAC =
  (DESCRIPTION =
     (ADDRESS = (PROTOCOL = TCP)(HOST = orarac1-vip)(PORT = 1521))
     (ADDRESS = (PROTOCOL = TCP)(HOST = orarac2-vip)(PORT = 1521))
     (LOAD_BALANCE=YES)
  (CONNECT_DATA=
     (SERVER=DEDICATED)
     (SERVICE_NAME=ORARAC)
  (FAILOVER_MODE=
     (TYPE=session)
     (METHOD=basic)
     (RETRIES=180)
     (DELAY=5)
  )
      )
    )
    
6.1.3 Service-Side TAF

TAF需要客户端配置tnsnames,非常麻烦,有一种只需要在服务器端配置故障转移的选项而不需要配置客户端的方法,即Service-Side TAF。

实现方法为:在实例上创建服务,为服务配置故障转移选项,客户端直接通过服务连接到数据库。



7、RAC环境中利用服务实现故障转移的实验

首先查看服务配置:

[oracle@node1 ~]$ srvctl status service -d orarac -s test_service
服务 test_service 正在实例 orarac2 上运行
[oracle@node1 ~]$ srvctl config service -d orarac -s test_service
服务名: test_service
服务已启用
服务器池: orarac_test_service
基数: 1
断开连接: 假
服务角色: PRIMARY
管理策略: AUTOMATIC
DTP 事务处理: 假
AQ HA 通知: 假
故障转移类型: SELECT
故障转移方法: BASIC
TAF 故障转移重试次数: 0
TAF 故障转移延迟: 0
连接负载平衡目标: LONG
运行时负载平衡目标: NONE
TAF 策略规范: BASIC
首选实例: orarac2
可用实例: orarac1

可以看出故障转移的方法及类型:
故障转移类型: SELECT
故障转移方法: BASIC

客户端tns的配置如下:

TEST_SERVICE =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = cluster-scan)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = test_service)
    )
  )
 
客户端连接数据库:

C:\Documents and Settings\Administrator>sqlplus system/oracle@test_service

SQL*Plus: Release 10.2.0.1.0 - Production on Tue Dec 2 09:49:16 2014

Copyright (c) 1982, 2005, Oracle.  All rights reserved.

Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
With the Partitioning, Real Application Clusters, Automatic Storage Management,
OLAP,
Data Mining and Real Application Testing options

SQL> select instance_name from v$instance;

INSTANCE_NAME
----------------
orarac2

为了创建长时间查询先创建大表:

SQL> create table testuser.object as select * from dba_objects;

Table created.

SQL> insert into testuser.object select * from dba_objects;

72252 rows created.

insert语句多执行几次

SQL> select count(*) from testuser.object;

  COUNT(*)
----------
   1156032
   
执行长查询:

SQL> select * from testuser.object;

中途关闭orarac2(使用sqlplus关闭),客户端显示:

ERROR:
ORA-25402: transaction must roll back

855 rows selected.

原因是事务未提交,必须回滚。此时客户端再执行任何查询仍然报错:

SQL> select instance_name from v$instance;
select instance_name from v$instance
*
ERROR at line 1:
ORA-25402: transaction must roll back

回滚事务:

SQL> rollback;

Rollback complete.

客户端在执行查询:

SQL> select instance_name from v$instance;

INSTANCE_NAME
----------------
orarac1

服务被转移到了orarac1上运行了,并且客户端session并未中断。

将上述过程重来一遍,但是在长查询之前进行提交:

先将orarac2启动,然后手动重定位服务


[oracle@node1 ~]$ srvctl status service -d orarac -s test_service
服务 test_service 正在实例 orarac1 上运行
[oracle@node1 ~]$ srvctl relocate service -d orarac -s test_service -h

将服务从集群的一个节点临时重新定位到另一个节点(命令忘记了,中途查一下帮助)。

[oracle@node1 ~]$ srvctl status service -d orarac -s test_service
服务 test_service 正在实例 orarac1 上运行
[oracle@node1 ~]$ srvctl relocate service -d orarac -s test_service -h

用法: srvctl relocate service -d <db_unique_name> -s <service_name> {-i <old_inst_name> -t <new_inst_name> | -c <current_node> -n <target_node>} [-f]
       为管理员管理的数据库指定实例, 或者为策略管理的数据库指定节点
    -d <db_unique_name>      数据库的唯一名称
    -s <service>             服务名
    -i <old_inst>            旧的实例名
    -t <new_inst>            新的实例名
    -c <current_node>        要从中重新定位服务的节点名
    -n <target_node>         要将服务重新定位到的节点名
    -f                       在停止或重新定位服务操作期间断开所有会话
    -h                       输出用法
[oracle@node1 ~]$ srvctl relocate service -d orarac -s test_service -i orarac1 -t orarac2
[oracle@node1 ~]$ srvctl status service -d orarac -s test_service
服务 test_service 正在实例 orarac2 上运行

此时验证一下客户端session是否正常:

SQL> select count(*) from testuser.object;

  COUNT(*)
----------
     72252
    
没有问题。

SQL> select instance_name from v$instance;

INSTANCE_NAME
----------------
orarac1

虽然relocate了服务到orarac2上,但是已经连接的session仍然处于orarac1上。

重新连接sqlplus确认连接到orarac2上,并确认不存在未提交事务:

SQL> commit;

Commit complete.

这一次只使用72252行,此时没有未提交事务。

执行长查询之前确认连接实例:

SQL> select instance_name from v$instance;

INSTANCE_NAME
----------------
orarac2

SQL> select * from testuser.object;

在服务器上关闭实例2:

SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.

客户端查询仍在进行,等待运行结果:

......
72252 rows selected.


看到客户端查询正常完成,和定义的TYPE=SELECT结果相一致。


8、 一些关于服务故障转移的结论

测试版本为11.2.0.1;节点为node1,node2;数据库名为orarac;实例名为orarac1,orarac2;服务名为test_service;服务配置信息见上面说明。

通过上述实验可以得出下面结论:

通过服务可以定义负载均衡及故障转移,而不需要在客户端做任务配置。

srvctl启动首选实例,服务会自动启动。


srvctl关闭服务的首选实例,服务不会自动转移到备选实例。


sqlplus中启动首选实例,服务不会自动启动。


sqlplus中关闭首选实例,服务会自动转移到备选实例。


服务转移到备用实例后,此时首选实例恢复正常,服务不会自动回到首选实例。


客户端session连接到服务后,如果此时通过srvctl relocate将服务转移到其他实例,客户端连接的实例仍然不变。此时故障转移仍然正常。


如果事务未提交,此时发生故障转移,则必须回滚事务,才能进行其他操作。

如果有朋友测试的结果和上述不一样,请留言说明,非常感谢。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值